PDA

View Full Version : Java RESTful and Extjs succes not enterpreted right in Response on POST request



oe.elvik
8 Feb 2011, 1:21 AM
I'm trying to create an application using java restful serverside an extjs as client, im using json to transfer data.

To produce and consume json serverside i use javax.xml.bind.annotation (@XmlRootElement, @XmlElement) ...

My problem is when inserting an new entity into my JsonStore. I figured out that extjs need a response something similar to this: {success: true , data: { id: [someId] }} My problem is that the server produses something like : {"success":"true" .... } (Notice the duble-quotes) extjs wont interpret the dubble-quoted boolean value off success, and therfore wont evaluete the request as successful.

I use a responsebuilder to create the response.

ResponseBuilder response = Response.created(uriInfo.getAbsolutePath().resolve(data.getId() + "/"));
response.entity(new MyResponse(true, data));
return response.build();

MyResponse is a xml annotated class.

I've tried response.entity("{success: true, data: {id:" + data.getId() + "}"); and this makes extjs evaluate the request as succesessful, but all the rest of the fields in my dataobject gets reset to null.
I guess i could pass all the fields like i did with the id, but that would be a far from elegant and make it harder to continue developing.

I tried to search for samples on this, but could not find any. Anyone of you tried this before and have a solution?

Should I fix this in javascript, is there a method to preprocess the response? Or is there a better way serverside? Any way i can use javax.xml.bind.annotations to overcome this, or any suggestions how to use a json marshaller that dont add dubble-quotes for boolean values.

oe.elvik
8 Feb 2011, 1:56 AM
I finaly found a solution to this problem my selfe..

Seems like a miracle happened when i finaly filed a help request..

The solution was to add a configuration file:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Meso;

import Meso.collections.Contacts;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;

/**
*
* @author develop
*/
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {

private JAXBContext context;
private Class[] types = {Contacts.class};

public JAXBContextResolver() throws Exception {
this.context =
new JSONJAXBContext(JSONConfiguration.natural().build(), types);
}

@Override
public JAXBContext getContext(Class<?> objectType) {
for (Class type : types) {
if (type == objectType) {
return context;
}
}
return null;
}
}

It is the configuration of using JSONConfiguration.natural().build() (natural nor mapped) that overcomes this problem.