简体   繁体   中英

Jersey application - 415 unsupported media type

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?

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM