@Consumes({"application/xml,application/json"}) how to program the return type

21,439

Solution 1

The 415 Unsupported Media Type is usually because, on your client request, you did not set the proper media type headers. In this case, you need a Content-Type: application/xml or Content-Type: application/json in either your XML or JSON request.

JAX-RS depends on the Content-Type request header to find the proper JAX-RS Provider to unmarshal the incoming request.

Solution 2

That's part of the beauty of Jax-RS - Jaxb annotate your POJO and jax-rs will handle the marshalling and unmarshalling to/from xml/json. You don't have to do that, the provider is supposed to handle a defined subset, of which, JSON and XML are part of.

To answer your second part of the question - the return type is determined by content negotiation process. The client can send the "Accept" header to say what type they want the response in. Without a 'suggestion' from the client, the server is left to try and pick a suitable return type.

Share:
21,439
user1801279
Author by

user1801279

Code Jedi

Updated on February 05, 2020

Comments

  • user1801279
    user1801279 about 4 years

    I have an application and I want it to accept both XML and JSON , how can I program the return type ? for example this is my POJO

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    
    // Class to marshall and unmarshall the XML and JSON to POJO
    
     // This is a class for the request JSON and XML
    
    
    @XmlRootElement
    public class KeyProvision {
    
        private String Consumer ; 
        private String API ; 
        private String AllowedNames ; 
    
    
        public void setConsumer( String Consumer)
        {
            this.Consumer= Consumer;
    
        }
    
    
        public void setAPI( String API){
    
            this.API = API;
    
        }
    
    
        public void setAllowedNames(String AllowedNames){
    
            this.AllowedNames = AllowedNames;
    
        }
    
         @XmlElement(name="Consumer")
        public String  getConsumer(){
    
            return Consumer;
        }
    
         @XmlElement(name="API")
        public String getAPI(){
    
            return API;
        }
    
         @XmlElement(name="AllowedNames")
        public String getAllowedNames(){
    
            return AllowedNames;
        }
    
    }
    

    My rest interface is

        import javax.ws.rs.Consumes;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    
    @POST
         @Path("/request")
         @Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
         @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
         public Response getRequest(KeyProvision keyInfo){
    
        /* StringReader reader = new StringReader(keyInfo); // this code just leads to an execution failure for some reason 
         try{
             JAXBContext jaxbContext = JAXBContext.newInstance(KeyProvision.class);
    
             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
             KeyProvision api = (KeyProvision) jaxbUnmarshaller.unmarshal(reader);
             System.out.println(api);
    
         }   catch(JAXBException e){
             e.printStackTrace();
    
         }
          */
    
         String result = "Track saved : " + keyInfo;
         return Response.status(201).entity(result).build() ;
    
      //   return "success" ;
    
     }
    

    my XML is

    <?xml version="1.0" encoding="UTF-8"?>
    <KeyProvision>
    <Consumer> testConsumer </Consumer>
    <API>posting</API>
    <AllowedNames> google</AllowedNames>
    </KeyProvision>
    

    my JSON is

    {
        "KeyProvision": {
            "Consumer": "testConsumer",
            "API": "posting",
            "AllowedNames": "google",
    
        }
    }
    

    My problems/questions are

    1) I keep getting an 415 error when I use the JSON , why is this not unmarshalling properly? 2) Is the reuturn type determined by JAXB?

    • Josh M
      Josh M about 10 years
      Not relevant to your problem but you should consider taking a closer look at your setAllowedNames(String) method.
  • user1801279
    user1801279 about 10 years
    Actually I should have metioned this earlier . I did set the content type in my request , it still throws the error which is why it has had me baffled
  • Bryant Luk
    Bryant Luk about 10 years
    Do you have all the JARs included in the class path for JSON? I don't know which JSON library you are using (Jackson, Jettison, or something else) but it could be that the JAX-RS JSON provider isn't registering correctly with the runtime.