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?