/ #Java #Jersey 

A Jersey POJOMapping Example in Mapping Form Parameters

Jersey, RESTful Web Services in Java.

In Java Servlet circumstance, we usually harvest the form parameters by using request.getParameter(“FORM_FIELD_NAME”) syntax. Now we can do it more elegant while enabling Jsersey’s POJOMapping features. The following example demonstrates the account registration scenario. Here we have a Account class, i.e., Account.java:

public class Account {
    private String email;
    public String getEmail() {
        return email;
    public void setEmail(String email) {
        this.email = email;

Make sure you have turned on the POJOMapping feature in web.xml,

    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-name>Jersey REST Service</servlet-name>

Account registration service with http POST method in AccountResource.java:

public class AccountResource {
    public Response Register(InputStream is){
        Response response = null;
        try {
            Account acct = new ObjectMapper().readValue(is, Account.class);         
        } catch (IOException e) {
            response = Response.serverError().build();
        if(response == null)
            response = Response.ok().build();
        return response;                

As shown in above code, we can read value from input steam into custom class (i.e., Account class) then continue doing the following business logic. However, you may encounter the Unsupported Media Type status code (415) while using the static HTML form post method. This is because we identify the service-consume-type is JSON.

Unsupported Media Type

So we get correct response and mapping object while the corresponding way. Note that the differences in Content-Type and Request Payload parts.

Request Payload