Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Jersey interceptors to get request body
    text
    copied!<p>I am using <code>REST-Jersey</code> in my project. All the POST data is send in <code>JSON</code> format and unmarshalled at server-side into respective beans. Something like this:</p> <p>Sending request to server:</p> <pre><code> $('a#sayHelloPost').click(function(event){ event.preventDefault(); var mangaData = { title:'Bleach', author:'Kubo Tite' } var formData=JSON.stringify(mangaData); console.log(formData); $.ajax({ url:'rest/cred/sayposthello', type: 'POST', data: formData, dataType: 'json', contentType:'application/json' }) }); </code></pre> <p>Payload:</p> <pre><code>{"title":"Bleach","author":"Kubo Tite"} </code></pre> <p>Server-end:</p> <pre><code>@POST @Path("/sayposthello") @Produces(MediaType.APPLICATION_JSON) public Response sayPostHello(MangaBean mb){ System.out.println(mb); return Response.status(200).build(); } </code></pre> <p>MangaBean:</p> <pre><code>public class MangaBean { private String title; private String author; @Override public String toString() { return "MangaBean [title=" + title + ", author=" + author + "]"; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } } </code></pre> <p>Output on console:</p> <pre><code>MangaBean [title=Bleach, author=Kubo Tite] </code></pre> <p>I got the REST-interceptor implementation from <a href="https://stackoverflow.com/questions/4358213/how-does-one-intercept-a-request-during-the-jersey-lifecycle">here</a>.</p> <pre><code>public class JerseyFilter implements ContainerRequestFilter{ @Override public ContainerRequest filter(ContainerRequest req) { return req; } } </code></pre> <p>I want to access the payload(request body) in the interceptor. As the data is in JSON format, its not accessible as request parameters. <strong>Is there a way I can get the request body in the interceptor method?</strong> Please advice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload