user1579339
user1579339

Reputation:

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();

Upvotes: 5

Views: 8253

Answers (2)

GlennV
GlennV

Reputation: 3670

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.

Upvotes: 3

condit
condit

Reputation: 10962

You can return a Response object instead. Have a look at:
https://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e5169 and
http://jersey.java.net/nonav/apidocs/1.17/jersey/javax/ws/rs/core/Response.ResponseBuilder.html

These should get you on the right track...

Upvotes: 4

Related Questions