【发布时间】:2012-09-21 18:07:58
【问题描述】:
我在我的基于休息的网络服务中创建了以下休息方法
@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 发送?
【问题讨论】:
-
您是否尝试删除 @Produces 注释?
-
当我想要的只是一个 json 时,为什么我要删除 @Produces 注释?不管怎样,我试了一下......没有帮助。
-
是什么让您认为“hello world”不是有效的 JSON?
-
@albogdano
"hello world"是有效的 json,但hello world不是。他说端点返回后者