JSON unmarshalling using jaxb

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

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;


    public String  getConsumer(){

        return Consumer;

    public String getAPI(){

        return API;

    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;

     public Response getRequest(KeyProvision keyInfo){

    /* StringReader reader = new StringReader(keyInfo); // this code just leads to an execution failure for some reason 
         JAXBContext jaxbContext = JAXBContext.newInstance(KeyProvision.class);

         Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
         KeyProvision api = (KeyProvision) jaxbUnmarshaller.unmarshal(reader);

     }   catch(JAXBException e){


     String result = "Track saved : " + keyInfo;
     return Response.status(201).entity(result).build() ;

  //   return "success" ;


my XML is

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

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




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


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.

Try adding this to your web.xml


More info Jersey JSON Support

Need Your Help

Laravel site not using Ajax in external javascript file

javascript php jquery ajax laravel-5

I am trying to have my site use Ajax from another file but it never works unless the code is actually in the view.

jquery to print from a modal window

jquery printing

I have couple modal windows and I am trying to add "print this" functionality. I found a script and I am trying to modify this. He creates an iframe, loads the html, prints and then closes the fram...