1

I have jersey application and i could not make form submit success. I get 415 unsupported media type error.

AngularJs:

$http.put(url, frmData, {
      transformRequest: angular.identity,
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      data: frmData
    });

Java:

@PUT
@Path("/submitform")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void saveForm(@Context HttpServletRequest request,
                     PojoClass pojoClass) {

}

I get 415 unsupported media type error.

Is there anywhere I am going wrong?

AJJ
  • 3,570
  • 7
  • 43
  • 76

2 Answers2

3

Without some tweeking, Jersey doesn't know how to convert application/x-www-form-urlencoded data into a POJO. The following are what it knows

public Response post(javax.ws.rs.core.Form form)

public Response post(MultivaluedMap<String, String> form)

public Response post(@FormParam("key1") key1, @FormParam("key2") String key)

If you want to use a POJO, you can annotate the POJO parameter with @BeanParam, and annotate the POJO fields with @FormParam

public class POJO {
    @FormParam("key1")
    private String key1;
    @FormParam("key2")
    private String key2;
    // getters/setters
}

public Response pose(@BeanParam POJO pojo)

If you are using Angular though, you might as well use JSON, as that's the default behavior. You may want to check out this post, if you want to work with JSON.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0
  1. Add @Consumes(MediaType.APPLICATION_JSON) in your saveFormMethod

  2. Instead of mapping pojoClass directly as a input parameter, change input parameter as a string and then use gson library to convert string back to actual POJO object.

shankarsh15
  • 1,947
  • 1
  • 11
  • 16