[英]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.