如何将整数(原始类型)作为响应json返回

时间:2015-11-06 12:57:28

标签: json jersey integer jax-rs

我有以下方法:

public class sum {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes("application/json")
    public responseObject sumOfTwo(@QueryParam("a") int a, @QueryParam("b") int b) {
        return new responseObject(a+b);
    }
}
@XmlRootElement
class responseObject{
    public int res;
    public responseObject(int res){
        this.res = res;
    }
    public responseObject(){
        //default C'tor
    }
    public int getRes(){
        return this.res;
    }
}

这将返回带有引号值的{"res":"value of a+b"}。我希望它将值作为整数返回,没有引号,但我将它作为字符串。如何序列化/反序列化它只输出一个整数?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用“整数”而不是原始的“整数”? 你的JSON提供商是什么?

我见过的大多数JSON序列化问题都可以通过将Json Provider从默认(MOxy)Json Provider更改为Jackson来解决。它将像你想象的那样处理bean序列化。另一方面,MOxy通常会对我的经历造成问题。

杰克逊如何使用泽西岛:https://jersey.java.net/documentation/latest/media.html#json.jackson