我有一个 RestService,我在其中返回一个 JSON 响应,如下所示。
下面是我的代码:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@GET
@Path("/json/lime")
@Produces(MediaType.APPLICATION_JSON)
public DataResponse getData(@Context DataInfo info) {
// some code here
System.out.println(resp.getResponse());
return new DataResponse(resp.getResponse());
}
下面是我在访问我的 Restful 服务后得到的实际 JSON 响应:
{
"response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}
这是它从 System.out 中打印出来的内容,如上所示:
{"ABC":100,"PQR":"40"}
这是我的 DataResponse
类:
public class DataResponse {
private String response;
public DataResponse(String response) {
this.response = response;
}
public String getResponse() {
return this.response;
}
}
如您所见,我的 response
字符串在上面的 JSON 中被转义了。我不确定为什么?任何人都可以帮助我理解为什么会发生这种情况以及如何解决它吗?
最佳答案
使用响应类:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@GET
@Path("/json/lime")
@Produces(MediaType.APPLICATION_JSON)
public ResponsegetData(@Context DataInfo info) {
...
return Response.status(200).entity(resp.getResponse()).build();
}
关于java - 如何将原始 JSON 作为来自 Restful 服务的响应传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945937/