简体   繁体   中英

Jersey: adding headers to a server response

I'm using Jersey, I have the following method:

@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:
I start the Jersey server in this way:

    final HttpServer server = HttpServerFactory.create(baseUrl);
    server.start();

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. It's similar to the @FormParam annotation.

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 . See sections 2.5 and 2.13.

For the Jersey 2 user guide , see chapter 3 and section 3.6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM