0

the following code is the pojo that i use it to return data from a restful Web service in spring:

public class GlobalSite {

@JsonSerialize(using = IntToStringSerializer.class, as=String.class)
public int id;

@JsonProperty("SiteName")
public String name;

public List<Login> logins;

public class IntToStringSerializer extends JsonSerializer<Integer> {

    @Override
    public void serialize(Integer tmpInt,
                          JsonGenerator jsonGenerator,
                          SerializerProvider serializerProvider)
            throws IOException, JsonProcessingException {
        jsonGenerator.writeObject(tmpInt.toString());
    }
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<Login> getLogins() {
    return logins;
}

public void setLogins(List<Login> logins) {
    this.logins = logins;
}

}

here is the web service code:

    @RequestMapping(value = {"/getglobal", "/getglobal/"}, method = RequestMethod.POST) //, produces = "application/json"
@ResponseBody
@WebMethod
public GlobalSite getGlobal(

        //Touraj: ****************************************

        //        This WebService is For JACKSON test

        // ***********************************************

        HttpSession session,
        HttpServletRequest request,
        HttpServletResponse response) {
    long fir = System.currentTimeMillis();

    GlobalSite gs = new GlobalSite();


    List<Login> loginList = new ArrayList<Login>();
    Login login= new Login();
    login.setEmail("toraj4567@yahoo.com");
    login.setPass1("123456");
    login.setMessage("msg1");

    Login login2= new Login();
    login2.setEmail("toraj45672@yahoo.com");
    login2.setPass1("1234567890");
    login2.setMessage("msg2");

    loginList.add(login);
    loginList.add(login2);

    gs.setId(1);
    gs.setName("GlobalSiteTest");


    gs.setLogins(loginList);

    return gs;

}

and here is part of my pom.xml

        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.1-1</version>
    </dependency>

and here is part of my dispatcher-servlet.xml:

    <mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

I am using spring mvc 4.2.1; when i build the project and call the web service with postman i get the following error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class net.google.newsite.form.GlobalSite

I have tried to override "public int id" with IntToStringSerializer to return it as string in generated json by web serivce like this: "id": "1", When i am not using @JsonSerialize i have no problem and no error but the id will be number in returned json not string. what is the coz of this exception?

Touraj Ebrahimi
  • 566
  • 4
  • 14
  • There is an issue concerning `org.springframework` and `com.fasterxml.jackson.core` in version 2.7.x. Please see this [question](http://stackoverflow.com/questions/34721851/spring-4-2-3-and-fasterxml-jackson-2-7-0-are-incompatible/34742244#34742244). It's true that it works in 4.2.4, but as you are using 4.2.1 you might be happy with earlier release 2.6.5 of jackson-databind. – MyBrainHurts Feb 21 '16 at 10:38
  • I tried jackson 2.6.5 but it did not solve the problem. – Touraj Ebrahimi Feb 21 '16 at 10:56
  • More descriptive exception is: Failed to evaluate serialization for type [class net.google.newsite.form.GlobalSite]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'net.google.newsite.form.GlobalSite$IntToStringSerializer': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.google.newsite.form.GlobalSite$IntToStringSerializer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: net.google.newsite.form.GlobalSite$IntToStringSerializer.() – Touraj Ebrahimi Feb 21 '16 at 12:15

0 Answers0