Returning a simple string in a Rest service doesn't return json?

I have created the following relaxation methods in my leisure based web service

@GET @Produces("application/json") @Path("plain") public String getPlain() { return "hello world"; } @GET @Produces("application/json") @Path("wrapper") public Response getWrapper() { return Response.ok(new Object(){ public String data = "hello world";}).build(); } 

When I call a regular service, it returns the raw string hello world , not a formatted json handler. However, wrapping the string in the object returns json {"data": "hello world"}

Why is he exhibiting this behavior? How to send plain String as json?

+4
source share
2 answers

I tried the option above, it does not work.

 String result="hello world"; return result; 

The reason String doesn't get automatic conversion seems to be due to the lack of @XmlRootElement. Based on the documentation in cxf http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-HandlingJAXBbeanswithoutXmlRootElementannotations , we need to use some jaxbElementClassMap. But unable to find more information.

0
source

try it

 @GET @Produces("application/json") @Path("plain") public String getPlain() { String result= "hello world"; return result; } 

JSON requires a key-value pair.

-2
source

Source: https://habr.com/ru/post/1435558/


All Articles