1

I'm creating a rest web service and i want to consume a json/xml object. I'm using jersey and when i try to post the request i obtain a 415 error unsopported media type. Here is the pom dependency

<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.22</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.22</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22</version>
    </dependency>

the web.xml

servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>
        org.glassfish.jersey.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            provider packages
        </param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>
            org.glassfish.jersey.media.multipart.MultiPartFeature; 
            org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature,
            org.glassfish.jersey.jackson.JacksonFeature
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

and the webservice

@POST
@Path("FooPath")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public void insertFoo(FooClass fc){
    System.out.println(fc.getFoo());
}

anyone can help me?

Luca
  • 321
  • 1
  • 3
  • 16
  • so how do you post the request? Its content-type is probably not xml nor json. – wero Nov 02 '15 at 14:12
  • for test i post the request with a html page. it's a simple form with this value method="post" enctype='application/json' – Luca Nov 02 '15 at 14:14

1 Answers1

1

You try to send a application/json request using a form submit with the forms enctype set to application/json.

Unfortunately enctype only supports these values:

  • application/x-www-form-urlencoded (the default)
  • multipart/form-data
  • text/plain (in HTML5)

The browser silently ignores the enctype and you server rejects the request since it is most likely application/x-www-form-urlencoded.

In order to send the request as json you need to create an appropriate Ajax request from Javascript. Here is an example how to do that.

Community
  • 1
  • 1
wero
  • 32,544
  • 3
  • 59
  • 84
  • @Luca I think you should first verify (e.g. using Firebug) that the content-type in your original request was not the intended `application/json` as I analyzed. This ends this question. If you have problems to get the Ajax request working you should post a new question. – wero Nov 02 '15 at 15:04
  • i tried with this but doesn't work var frm = $(document.myform); var dat = JSON.stringify(frm.serializeArray()); alert("I am about to POST this:\n\n" + dat); $.post( frm.attr("action"), dat, function(data) { alert("Response: " + data); } ); – Luca Nov 02 '15 at 15:05