Search code examples
jerseyjax-rsmedia-typehttp-status-code-415

getting http 415, Unsupported Media Type using text/xml


I have a jersey endpoint(JAX-RS) that I'm trying to hit with a text/xml req. I'm getting back an http 415 and I don't understand why. Here is the info. Any ideas? Thanks.

@Path("/bid")
@Produces("text/xml;charset=ISO-8859-1")
@Consumes({"text/xml", "application/xml"})
@Resource
public class BidController {

@RolesAllowed("blah")
@POST
public Response bid(final HttpServletRequest request) {

I am hitting it via Postman(REST client) and sending {"Content-Type":"text/xml"}

My POST body is definitely well formed xml.


Solution

  • You are getting a 415 response because JAX-RS does not know how to convert incoming XML into a HttpServletRequest.

    If you really want access to the request, then you need to annotate it with @javax.ws.rs.core.Context:

    @RolesAllowed("blah")
    @POST
    public Response bid(@Context final HttpServletRequest request) {
    

    However, as you say you're hitting it with text/xml, then you may actually want:

    @POST
    public Response bid(final MyRequest request) {
        ...
    }
    

    where MyRequest is declared something like:

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class MyRequest {
    
         @XmlElement
         int field1;
    
         @XmlElement
         String field2;
    
         ...
    }
    

    which corresponds to XML like:

    <MyRequest>
        <field1>11327</field1>
        <field2>some string
    </MyRequest>
    

    The JAX-RS specification requires implementations to be able to decode incoming text/xml and encode outgoing text/xml via JAXB.