简体   繁体   English

Jersey服务器,rest api:如何从响应正文中删除代码和类型?

[英]Jersey server, rest api : How to remove the code and type from the response body?

I'm trying to create a Rest Api using Jax-rs Jersey from a base code generated by swagger. 我正在尝试根据swagger生成的基本代码使用Jax-rs Jersey创建Rest Api。

The specifications are for exemple for a specific Request : Code : 200 Description : User token for login Schema : String 规范是针对特定请求的示例:代码:200说明:登录用户令牌架构:字符串

My problem is that the generated code use the class :javax.ws.rs.core.Response that should not be extended according to the documentation. 我的问题是,生成的代码使用了:javax.ws.rs.core.Response类,该类不应根据文档进行扩展。

I'm using this kind of code to build the response : 我正在使用这种代码来构建响应:

return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK,apiToken)).build(); 

The response generated looks like that : 生成的响应如下所示:

{"code":4,"type":"ok","message":"uHN2cE7REfZz1pD17ITa"}

When i only want to have :"uHN2cE7REfZz1pD17ITa" in the body. 当我只想在体内拥有:“ uHN2cE7REfZz1pD17ITa”时。 Is that possible using Jersey ? 使用Jersey可以吗? Or is this format part of the jax-rs specifications ? 还是这种格式是jax-rs规范的一部分?

Thank you. 谢谢。

ApiResponseMessage from Swagger does not extend Response from JAX-RS. Swagger的ApiResponseMessage不会扩展JAX-RS的Response Check the code and you will see that ApiResponseMessage is just a POJO. 检查代码 ,您将看到ApiResponseMessage只是一个POJO。 That is, the piece of code you posted in your question is just fine. 也就是说,您在问题中发布的代码段就可以了。

If you only need the token, you can use: 如果只需要令牌,则可以使用:

return Response.ok(apiToken).build();

The following gives you the same result: 以下给出了相同的结果:

return Response.ok().entity(apiToken).build();

Since your resource method will produce just a piece of text (not a valid JSON unless the piece of text is wrapped into quotes), the most suitable media type for the response would be text/plain . 由于您的资源方法将只生成一段文本(除非将一段文本用引号引起来,否则它不是有效的JSON),因此,最适合响应的媒体类型将是text/plain It could be achieved by either annotating the resource method with @Produces(MediaType.TEXT_PLAIN) or setting the media type in the response, as following: 可以通过使用@Produces(MediaType.TEXT_PLAIN)注释资源方法或在响应中设置媒体类型来实现,如下所示:

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getToken() {
    String apiToken = ...
    return Response.ok(apiToken).build();  
}
@GET
public Response getToken() {
    String apiToken = ...
    return Response.ok(apiToken, MediaType.TEXT_PLAIN).build();  
}

Alternatively you also could make your method return just a String : 另外,您也可以使您的方法仅返回String

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getToken() {
    String apiToken = ...
    return apiToken;  
}

JAX-RS does not require Request or Response in specific format for text,json, xml, or html fallowing a schema . 对于文本,json,xml或html模式而言,JAX-RS不需要特定格式的请求或响应。 But They all have to be well formated in according to their specifications. 但是它们都必须根据其规范进行格式化。

You can send text response like this in jersey like this 您可以像这样在运动衫中发送文本回复

return Response.ok().entity("uHN2cE7REfZz1pD17ITa").build();

I am new to swagger myself So i don't know if the Response in question can be changed or not . 我是个新手,我不知所措,所以我不知道所讨论的响应是否可以更改。 But There is no restriction from jersey side 但是从球衣方面没有限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jersey REST响应消息正文 - Jersey REST response message body java - 当响应代码为 4xx 时,如何从 REST API 解析响应正文 - How to parse reponse body from REST API when response code is 4xx in java 如何在REST服务的RESPONSE主体中删除Cookie - How to Remove Cookies in RESPONSE Body of REST service 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 如何从Rest API登录的泽西岛客户端响应中获取Cookie到期日期? - How to get cookie expiry date from jersey client response from rest api login? 在REST客户端和Jersey Java代码完成的REST调用响应中获取不同的内容类型 - Getting the different Content Type in the response of REST call done by REST client and Jersey Java Code 如何使用多级资源编写Java jersey REST API? - How to code java jersey REST API with multi-leveled resources? 如何验证响应并从 Rest Assured 中的响应正文中提取值? - How to validate a response and extract a value from Response body in Rest Assured? 当 spring REST api 中缺少 header 时如何返回响应正文 - how to return a response body when header is missing in spring REST api 如何从Jersey 400响应代码返回XML - How to return XML from a Jersey 400 response code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM