I am trying to build a JSON string as server response with the aid of the Jackson library. If the route is not 0 I am getting tis response {"status":201,"routes":null} but I have problem to set the status variable to 204 in the SDBean class if the routeD is 0 I am getting in the console this output {"status":204,"routes":[1,9,3]} but in POSTMAN chrome extension the respose takes too long and I am getting this output The response status was 0.
Check out the W3C XMLHttpRequest Level 2 spec for more details about when this happens.
I want to get the following if routeD is not 0 to get {"status":201} else {"routes": {1,3,9}}
How can I manage that with Jakson?
Receiver class:
@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 class:
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;
}
}