如果未设置Http状态代码,请设置它

时间:2015-05-20 18:13:10

标签: java jersey jackson

我试图在Jackson库的帮助下构建一个JSON字符串作为服务器响应。如果路线不是0,我会收到回复{"status":201,"routes":null},但如果204我在SDBean课程中将状态变量设置为routeD则会出现问题是0我在控制台中输入了这个输出{"status":204,"routes":[1,9,3]}但是在POSTMAN chrome扩展名中,respose需要的时间太长而且我得到的输出The response status was 0. Check out the W3C XMLHttpRequest Level 2 spec for more details about when this happens. 如果routeD不为0,我想获得以下内容{"status":201}其他{"routes": {1,3,9}}

我如何使用Jakson管理它?

接收者类:

@Path("/data")
public class Receiver {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response storeData(Data data) {
        Database db = new Database();

        String macD = data.getMac();
        int routeD = data.getRoute();
        double latD = data.getLatitude();
        double longD = data.getLongitude();
        double speedD = data.getSpeed();

        SDBean bean = new SDBean();
        if (routeD != 0) {
            bean.status = db.insertData(macD, routeD, latD, longD);
            return Response.status(bean.status).entity(bean.toJson()).build();

        } else {
            bean.routes = db.detectRoute(latD, longD);
            return Response.status(bean.status).entity(bean.toJson()).build();

        }

    }

}

SDBean类:

import java.util.ArrayList;

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

public class SDBean {

    public int status;
    public ArrayList<Integer> routes;

    public String toJson() {

        ObjectMapper mapper = new ObjectMapper();
        String json = null;

        if(status == 0){
            this.status = 204;
        }

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

        }
        return json;
    }

}

1 个答案:

答案 0 :(得分:1)

由于你没有在bean中初始化你的int值,你的0响应问题。当您尝试分析代码时:

//Inside Receiver 
return Response.status(bean.status).entity(bean.toJson()).build();

在这一行中,您首先从bean的status字段获取值。它使用默认值0(Oracle documentation about primitive data types and default values)进行初始化,然后将其映射到Json(),在此处将其值设置为此行中的状态响应204:

//Inside SDBean
if(status == 0){
    this.status = 204;
}

我建议其中一个结果:

  1. 初始化变量内联:

    public int status = 204;

  2. 如果您需要一些额外的逻辑,可以使用静态初始化块

    static {
        //additional logic here
        status = 204;
    }
    
  3. 或者只是在你的情况下使用构造函数:

    public SDBean(){
        //additional logic here
        status = 204;
    }
    
  4. 有关详细信息,请提供文档:Initializing fields

    此外,您不能向ArrayList添加任何内容。尝试在bean内联中初始化它:

    public ArrayList<Integer> routes = new ArrayList<Integer>();
    

    或者如果你使用jdk 1.7+而不是使用钻石干扰

    public ArrayList<Integer> routes = new ArrayList<>();
    

    现在您可以使用它在storeData类中添加数据:

    bean.routes.add(yourIntValue);
    

    //编辑:

    package com.gmail.at.mironiuk.kacper.stack.overflow.wtf;
    
    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 {
    
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        public Response storeData(Data data) {
            Database db = new Database();
            SDBean bean = new SDBean();
    
            String macD = data.getMac();
            int routeD = data.getRoute();
            double latD = data.getLatitude();
            double longD = data.getLongitude();
            double speedD = data.getSpeed();
    
            if (routeD != 0) { // here you always will have null or empty case you only set your status here
                bean.status = db.insertData(macD, routeD, latD, longD); // In this if try to add sth to your list
                bean.status = 204;
                return Response.status(bean.status).entity(bean.toJson()).build(); //Expected result {"status":"204", "routes": []}
            } else { // here your List will be this whats you returns from
                bean.routes = db.detectRoute(latD, longD); // <- this function and i don't know what function returns
                return Response.status(bean.status).entity(bean.toJson()).build(); //Expected result {"status":"204", "routes": [?,?,?]}
            }
        }
    
    }
    

    我希望我帮助你