Jackson @JsonSerialize pojo转换异常

时间:2016-02-21 08:37:45

标签: java json rest spring-mvc jackson

以下代码是我用它在春天从restful Web服务返回数据的pojo:

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;
}

}

这是网络服务代码:

    @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;

}

这是我的pom.xml的一部分

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

这是我的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>

我正在使用spring mvc 4.2.1;当我构建项目并使用邮递员调用Web服务时,我收到以下错误:

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

我试图用IntToStringSerializer覆盖“public int id”,将它作为字符串在生成的json中通过web服务返回,如下所示:“id”:“1”,当我不使用@JsonSerialize时,我没有问题,没有错误,但id将是返回的json中的数字而不是字符串。这个例外的原因是什么?

0 个答案:

没有答案