0
votes

I am trying to make a jersey based web service. In this if i take input params using @FormParam it works fine:

@POST
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, "application/x-www-form-urlencoded"})
@Path("/registeruser")
public Response registerUser(@FormParam ("email") String email,@FormParam ("name") String name ){
    System.out.println("Inside register device");
    System.out.println("registered" + email);
    return null;
}

but when I try using @BeanParam it does not works and gives me an exception

@POST
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, "application/x-www-form-urlencoded"})
@Path("/registeruser")
public Response registerUser(@BeanParam UserForm userForm ){
    System.out.println("Inside register device");
    service.registerUser(userForm);
    System.out.println("registered" + userForm.getEmail());
    return null;
}

A message body reader for Java class com.stc.dms.forms.UserForm, and Java type class com.stc.dms.forms.UserForm, and MIME media type application/octet-stream was not found.

1
1) why are you accepting a request body for a GET request? 2) what jersey version are you using? 3) have you been able to get @BeanParam to work in other instance, say with some @QueryParams? - Paul Samsotha
No i have not been able to do it. Its POST request only by mistake i entered GET - user3529381
Can you try it right now. Make a different GET method, with a bean with just a couple @QueryParams. - Paul Samsotha
I just tried it with @QueryParam and it works fine bt i dont want to keep adding parameters like this. I want to Object as input - user3529381
Why are you consuming so many different media types? Have to tried to limit it to APPLICATION_FORM_URLENCODED, and making sure the on the client the Content-Type is set to application/x-www-form-urlencded? - Paul Samsotha

1 Answers

0
votes

You don't need to use @BeanParam to pass an object as input. Just pass it like this :

@POST
@Path("register")
@Consumes(MediaType.APPLICATION_JSON)
public Response registerUser(UserForm dto) {
  // ...
}

Just make sure to include the libraries for producing/consuming json. If the client is in javascript you don't need anything else (just use JSON.stringify() on your form object), for the server add some json libraries such as Jackson

EDIT : If you want to stick with @BeanParam, take a look at this tutorial here. Basically it seems that you don't need to specify the mediatype, as Jersey will handle that for you.