简体   繁体   中英

Rest web service consuming JSON using Jersey and ExtJS

I'm trying to implement the Jersey framework in order to route our client requests using the restful approach. I have successfully created methods that read primitive parameters, send them and they can even respond with a JSON from an object. However the one thing I have not been able to do is read (ie consume) a json object from a post/put or even get request. I've tried everything I can find online but to no avail. Because of some work issues I am confined using Java 6 and Jersey 1.19. Also for our client side stuff we're using ExtJS.

My ExtJS request is formatted as so:

Ext.Ajax.request({
url: 'rest/RestClass/getMessage',
method: 'PUT',
header: {
    'Content-Type': 'application/json'
},
jsonData: {
    status: {
        id: 1,
        description: 'Description'
    }
},
success: function(response) {
    var result = response.responseText;
},
failure: function(response) {
    Ext.Msg.alert('Communication Error', 'Failed');
}
});

As far as my back end stuff goes I have this:

@PUT
@Path("/getMessage")
@Consumes(MediaType.APPLICATION_JSON)
public void getMsg(JAXBElement<Status> status) {
    System.out.println("status id = " + status.getValue().getId());
    System.out.println("status description = " + status.getValue().getDescription());
}

Also my Status object is:

@XmlRootElement
public class Status{

    private int id;
    private String description;

    public Status() {

    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}

I have properly mapped the jersey servlet etc since I've already sent successful requests. My problem is when I try to send one with a json object in the parameters I get errors.

At first I was getting as error for a missing jackson library even though I am using jersey-bundle-1.19.jar which supposedly contains everything needed. After downloading jackson-jaxrs-1.9.13.jar and adding it to the project I am now getting this error:

A message body reader for Java class javax.xml.bind.JAXBElement, and Java type javax.xml.bind.JAXBElement, and MIME media type application/json was not found.

I even added some jaxb jars just in case but still getting the same error. Many examples I've seen online didn't work for me. Also they all use maven for dependency control which we do not have access to so I cannot be sure we are using the same libraries...

What I'm looking for here is a solution with the things I have available: jersey 1.19 ExtJS 5 JDK 6 WebLogic 10.X

The goal is to be able to send an request with a JSONObject as parameter and have it be decoded server side into an existing object type.

EDIT: I even added POJO mapping in my web.xml (i found online it is needed for jersey to support json/object mapping)

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

Still have the same problem. The client gives me a 415 error for unsupported media type

I found the solution to my problem. There are several changes that need to be made:

In the jsonData object of the request there shouldn't be a status root property. Just the object itself:

jsonData: {
    id: 1,
    description: 'Description'
},

Also I removed the JAXBElement<Status> from my method's declaration. It just receives a Status status object.

Finally the Status class doesn't need the @XmlRootElement annotations. It needs however the @JsonCreator annotation over its default (empty) constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM