Java 从JSON响应中删除密钥

Java 从JSON响应中删除密钥,java,jersey,jackson,Java,Jersey,Jackson,我正在使用Jersey框架并试图返回{“status”:201}作为响应,但此时我得到了{“status”:201,“route”:0}。在另一个响应中,我需要SDBeanPost类中的路线和方向,但不是在所有响应中。因此,我能够通过添加这行bean.direction=null,从响应中删除方向但我不知道如何从响应中删除路由,而不从SDBeanPost中删除route 接收器类别 package org.busTracker.trackingService; /* * Inside thi

我正在使用
Jersey
框架并试图返回
{“status”:201}
作为响应,但此时我得到了
{“status”:201,“route”:0}
。在另一个响应中,我需要
SDBeanPost
类中的路线和方向,但不是在所有响应中。因此,我能够通过添加这行
bean.direction=null,从响应中删除方向
但我不知道如何从响应中删除路由,而不从
SDBeanPost
中删除
route

接收器类别

package org.busTracker.trackingService;

/*
 * Inside this class, the request from the Android application - PostData class is received.
 *  Consequently, a response is sent back.
 */

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/data")
public class Receiver {
    static String flagD;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response proccessData(Data data) {

        Database db = new Database();

        String macD = data.getMac();
        int routeD = data.getRoute();
        double latD = data.getLatitude();
        double longD = data.getLongitude();
        String timeD = data.getTime();
        int speedD = data.getSpeed();
        String directionD = data.getDirection();
        flagD = data.getFlag();

        // Jackson class to wrapper the data as JSON string.
        SDBeanPost bean = new SDBeanPost();
        bean.direction = null;      
        bean.status = 201;

        return Response.status(bean.status).entity(bean.toJson()).build();

    }

}
SDBeanPost

package org.busTracker.trackingService;

/*
 * Class to wrap the response for the Receiver class. 
 */

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

//To return status without route null.
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SDBeanPost {

    public int status;
    public int route;
    public String direction;

    public SDBeanPost() {
        Receiver receiver = new Receiver();

        direction = "";
        status = 230;

    }

    public String toJson() {

        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();

        }
        return json;
    }

}

route是类中的一个基元
int
。因此,当您没有显式设置它时,它的实例将具有默认值0,因此,当序列化为json时,它将进入json输出


也许您可以对非必填字段使用装箱整数(Integer),或者使用
@JsonIgnoreProperties({“route”})
,以便Jackson忽略它。

route是类中的一个基元
int
。因此,当您没有显式设置它时,它的实例将具有默认值0,因此,当序列化为json时,它将进入json输出


也许您可以对非必填字段使用装箱整数(Integer),或者使用
@JsonIgnoreProperties({“route”})
以便Jackson忽略它。

既然您实际上只想返回一个状态,为什么要返回一个具有状态、路由和方向的SDBeanPost实例?使用另一个类,该类包含所需的字段,并且仅包含这些字段。另外,不要自己序列化为JSON。泽西是为你做的。顺便说一句,在正文中返回状态也是可疑的。HTTP响应已经有状态。因此,除非主体中的状态是一种完全不同的状态,否则它不应该在主体中。既然您实际上只想返回一个状态,为什么要返回一个具有状态、路由和方向的SDBeanPost实例?使用另一个类,该类包含所需的字段,并且仅包含这些字段。另外,不要自己序列化为JSON。泽西是为你做的。顺便说一句,在正文中返回状态也是可疑的。HTTP响应已经有状态。因此,除非主体中的状态是一种完全不同的状态,否则它不应该在主体中。我在这行
@JsonInclude(JsonInclude.Include.NON\u NULL)后面的
@JsonIgnoreProperties({“route”})
中尝试了
@JsonInclude.Include.NON\u NULL)
但我收到此错误
JsonIgnoreProperties无法解析为类型
。如果路由和方向在请求中有值,我只想从响应中删除路由和方向。否则,如果路由有
0
,并且在请求中方向有
也是空的,我想返回
{“status”:230,“route”:0。“direction”:“}
。只需检查是否导入了JsonIgnoreProperties。另外,如果您想在设置路由时发送路由,请在路由的SDBeanPost中使用
Integer
而不是
int
,在这行
@JsonInclude(JsonInclude.Include.NON\u NULL)之后的
SDBeanPost中使用
int
但我收到此错误
JsonIgnoreProperties无法解析为类型
。如果路由和方向在请求中有值,我只想从响应中删除路由和方向。否则,如果路由有
0
,并且在请求中方向有
也是空的,我想返回
{“status”:230,“route”:0。“direction”:“}
。只需检查是否导入了JsonIgnoreProperties。此外,如果要在设置路由时发送路由,请在路由的SDBeanPost中使用
Integer
而不是
int