0

I have an application and I want it to accept both XML and JSON , 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?

my dependencies are

   <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>

    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>

I set up the header as Content-Type:application/json

8
  • 415 is media unsupported, are you setting up the content type in your request? Commented Feb 5, 2014 at 21:23
  • 1
    When you try Json from the client - are you setting the Accept header to "application/json" - because your default order is xml first then json - so unless you specifically ask for json, the resource class will default to xml. Commented Feb 5, 2014 at 21:26
  • @mikemil yes , I set the header to application/json . Infact I know it accepts it because if I change the method to accept a string instead of the object I can see the request as a String . There is a problem with the unmarshalling Commented Feb 5, 2014 at 21:29
  • Just another thought - it looks like you are returning a String - the field is 'result' not the KeyProvision. Commented Feb 5, 2014 at 21:40
  • @mikemil I am confused on why the return type would be the issue , The problem I face is with the unmarshalling . It is important for me to get the info . the return type could be anything Commented Feb 5, 2014 at 21:46

2 Answers 2

1

I would take a guess that you are not setting the content type correctly on your http request which you are sending to the service.

I dont know what tool you are using for creating a request i would recommend:

  • Cmd line - curl. you just cant beat it! Chrome
  • Advanced Rest Client. Simply the best when working with rest services.

Both of these will allow you to see the raw http request and response and thus diagnose your problem.

Generall as a rule of thumb a space between colon and value when setting a request header is preferable. i.e

Content-Type: application/json;

So a sample curl request would look like this:

curl -X POST -H "Content-Type: application/json" -d '{"KeyProvision":{"Consumer":"testConsumer","API":"posting","AllowedNames":"google"}}' http://localhost:8080/request

Curl will then spit out both raw request and response so you can clearly see what's happening. Advanced rest client does same but also got many tools for remembering and viewing responses/requests.

Sign up to request clarification or add additional context in comments.

Comments

0

Try adding this to your web.xml

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

More info Jersey JSON Support

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.