0
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Path("/data/services")
public Response  DiscoverDevice(BlockDevsPost blockdevice) {

    for (DeviceIdentifier device : blockdevice.getDevice()) {
        String dev = device.Device();
        System.out.println("DEVICE "+ dev);
        if (dev == null || dev.equals("")){
            return Response.status(Response.Status.BAD_REQUEST).entity("Device cannot be null or empty.").build();
        }
    }
}

Getting this error when fired POST from REST Client when dev is null. I am not able to get JSON and this error is thrown:

Unexpected character (D) at position 0. Device Identifier cannot be null or empty.

Where D in Device Identifier marked as Red which means it is not returning JSON as response.

3
  • I am not able to get JSON an this error is thrown when dev is null Commented Feb 17, 2017 at 10:34
  • please clarify your question Commented Feb 17, 2017 at 12:21
  • When dev is sent as null from REST Client . It will throw the error as HTTP Bad Request error . But the response message I am getting - Unexpected character (D) at position 0. Device Identifier cannot be null or empty. where D in Device Identifier marked as Red which means it is not returning JSON as response. Commented Feb 18, 2017 at 3:09

1 Answer 1

1

Your client is expecting to get JSON but you have set a plain string in the Response entity and application/json as content-type. You need to return a valid JSON. For example

return Response
           .status(Response.Status.BAD_REQUEST)
           .entity("{\"error\":\"Device cannot be null or empty.\"}")
           .build();

You can also build the json response string using your preferred mapper (you will need to add a dependency). This is an example using Jackson

Jackson using API

ObjectMapper mapper  = new ObjectMapper();
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("error", "Device cannot be null or empty.");
String json = mapper.writeValueAsString(objectNode);

Jackson using POJO

class ErrorBean{
   private String error;
   //getters and setters
}

ObjectMapper mapper = new ObjectMapper();
ErrorBeanerrorBean = new ErrorBean();
errorBean.setError ("Device cannot be null or empty.");
String json = mapper.writeValueAsString(errorBean);

You can also return POJO from your service method and let the JAX-RS implementation to convert them to JSON (this means change the response type). See https://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

Sign up to request clarification or add additional context in comments.

3 Comments

Worked !. Thank-you.
Escape of double quotes doesnt look proper. Is there any other way to write the wrapper to accept the JSON Object.
You can build the JSON string using a mapper library: Jackson, Gson, jettison... See an example in the updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.