我在基于休息的网络服务中创建了以下休息方法
@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();
}
当我调用普通服务时,它返回一个原始字符串 hello world 而不是json格式化输出。然而,将字符串包装在对象中会返回json {“data”:“hello world”}
为什么会出现这种行为?我如何将普通字符串作为json发送?
答案 0 :(得分:0)
我尝试了上面的选项,它不起作用。
String result="hello world";
return result;
String未被自动转换的原因似乎是由于缺少@XmlRootElement。根据cxf http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-HandlingJAXBbeanswithoutXmlRootElementannotations中的docuementation,我们需要使用一些jaxbElementClassMap。但无法找到更多细节。
答案 1 :(得分:-2)
试试这个
@GET
@Produces("application/json")
@Path("plain")
public String getPlain()
{
String result= "hello world";
return result;
}
JSON需要一个键值对。