【问题标题】:Howto add content to Response?如何向响应添加内容?
【发布时间】:2015-11-27 15:21:18
【问题描述】:

我的 WebService(我想把 uuid 字符串放在响应中)

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public Response createObject(final String jsonString) {
    String uuid;
    //logic
    ...
    String mt = new MimetypesFileTypeMap().getContentType(uuid);
    return Response.ok(uuid, mt).build();
    //also tried: Response.status(200).entity(uuid).build();
}

我的请求(从响应中读取 uuid)

Response response = target.request().post(Entity.entity(new Gson().toJson(params, Map.class), MediaType.TEXT_PLAIN));
response.getEntity() //returns null if i access it  

当我尝试访问实体时,它始终为空。我做错了什么?

【问题讨论】:

  • 您是否必须返回Response 对象?您可以简单地返回一个Stringpublic String createObject(..) {..return UUIDString;}
  • 当我返回字符串时它可以工作,但我还想检查客户端的 http(错误)状态代码
  • 尝试只返回一个实体,即return Response.ok(uuid).build();
  • 试过但同样的问题

标签: java web-services jax-rs jax-ws response


【解决方案1】:

您好,我发现了问题:

这是一个解析问题,不知何故我总是得到 null,直到我更改了我的请求代码

response.getEntity()

到这里:

String uuid = new Gson().fromJson(response.readEntity(String.class), String.class);

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2018-01-03
    • 2010-09-24
    • 1970-01-01
    • 2017-06-09
    相关资源
    最近更新 更多