[英]Jersey: adding headers to a server response
I'm using Jersey, I have the following method: 我正在使用Jersey,我有以下方法:
@POST
@Path("hello")
@Produces(MediaType.TEXT_HTML)
public String hello(@FormParam("username") String username)
{
Gson gson = new Gson();
CommunicationResponseM result = new CommunicationResponseM();
String result = "hello";
return gson.toJson(result);
}
So far all goes well, but now I need to add some headers. 到目前为止一切顺利,但现在我需要添加一些标题。 How can I do that?
我怎样才能做到这一点?
Thanks! 谢谢!
PS: PS:
I start the Jersey server in this way: 我以这种方式启动Jersey服务器:
final HttpServer server = HttpServerFactory.create(baseUrl);
server.start();
You can return a Response
object instead. 您可以改为返回
Response
对象。 Have a look at: 看一下:
https://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e5169 and https://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e5169和
http://jersey.java.net/nonav/apidocs/1.17/jersey/javax/ws/rs/core/Response.ResponseBuilder.html http://jersey.java.net/nonav/apidocs/1.17/jersey/javax/ws/rs/core/Response.ResponseBuilder.html
These should get you on the right track... 这些应该让你走上正轨......
If you're looking for a way to get the value of a header parameter from the http request, then you can use the @HeaderParam annotation. 如果您正在寻找从http请求获取标头参数值的方法,那么您可以使用@HeaderParam注释。 It's similar to the @FormParam annotation.
它类似于@FormParam注释。
If you're looking to add a header to your response, there are several ways. 如果您希望在回复中添加标题,可以采用多种方法。
For Jersey 1, there's more info in the Jersey 1.18 user guide . 对于Jersey 1, Jersey 1.18用户指南中有更多信息。 See sections 2.5 and 2.13.
见2.5和2.13节。
For the Jersey 2 user guide , see chapter 3 and section 3.6. 有关Jersey 2用户指南 ,请参阅第3章和第3.6节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.