how to create jackson based json rest service with Spring 2 - json

Spring 3 has native json support for returning json response using #ResponseBody spring 3 annotation.
My app is based on spring 2 and need to create jackson based rest service which will return json when client make http rest request using browser.
I am exploring how to achieve this. Any body suggestions on this is appreciated.
Thanks

In Spring 3 we have default converter which converts any object (using Jackson) to JSON. We can override this default converter to define some specials settings. In Spring 2 we can implement View. You can implement this view using Jackson library to convert any object to JSON.
Your controller could look like that:
public ModelAndView generateJson(.....) {
//business logic
return new ModelAndView(new JsonView(objectToConvert);
}

Related

How to manage request and response in JSON format using spring 3

I have to write a logic where I would be receiving JSON request and I have to return response in JSON format. I am using spring 3 and would be using restful controller in spring MVC using #responseBody annotation.
Please tell me what should be my approach with sample code and how can I test it without any front end available in best manner.
Json format is nothing but a string. You can use rest template for getting ur result. You have to set message converter bean in your context xml.
pls go through the link below
http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program

REST proxy for a SOAP Web Service: can I reuse the JAXB classes for the input/output JSON?

I'm creating a proxy service for translating an existing SOAP Web Service to REST. I mean, to create a REST Controller based on Spring for creating the REST interface that will call the existing SOAP Web Service.
The SOAP response must be translated into JSON on the REST service's response.
The steps I followed were:
I have generated the SOAP WebService classes thanks to CXF
(wsdl2java). OK.
I have created the REST Controller for invoking the existing SOAP WS with the previous classes. OK.
The input JSON parameter corresponds to the SOAP input parameter. Can I reuse the JAXB classes I generated on the wsdl2java process?
So I tried to define the REST controller as:
public #ResponseBody WebServiceJAXBOutput service(#RequestBody WebServiceJAXBInput input){
...
}
Nevertheless, the REST call always returns 400 (Bad request) if I specify the data values. Although it works if the input JSON fields are null:
{
"application":null,
"center":null,
"language":null
}
I guess the JAXB getter/setters are failing because of the JAXBElement (public JAXBElement getApplication()).
Should this approach work? Did I miss something?
Many thanks!!
Sergi

Mobile application + Spring MVC - JSP?

I've got a web application built over Spring MVC, already working. I'm planning to give mobile users an app for communicating with the server, so they will find easier to interact with it. I've got the Model, the Views and the Controllers working fine, but everything was designed from the web's perspective.
So, I'm building up a few new Controllers for the mobile app, and here comes the question: since the ultimate rresponsible of the View is going to be the mobile app in question, where should I delegate everything to the app, in the Controller (preparing there a JSON for each response)? Or should I have a JSP with some JSON taglib enabled, so that the Controller gives the pieces to the JSP, and then I build the JSON response in the JSP?
I'm not clear about the MVC architecture on this scenario.
Thanks in advance.
When all you are doing is creating a REST API which mobile (or other clients) are going to use, Views do not come into play. It's the responsibility of the Controller to prepare the appropriate response for the client.
Luckily since returning JSON is such a common scenario, Spring MVC transparantly handles the serialization into JSON (using the Jackson library) so you don't have to.
As JB Nizet showed, you can use the #ResponseBody annotation to tell Spring MVC that the response should be returned as is (serialized to JSON because of the produces = MediaType.APPLICATION_JSON_VALUE) or if you are using Spring 4, you can completely ditch the #ResponseBody annotation and annotate your Controller with #RestController (which makes Spring behave as if #ResponeBody was added to every method mapping) instead of #Controller.
The controller methods should simply return obejcts (or collections of objects), that will be serialized to JSON automatically thanks to the #ResponseBody annotation:
#RequestMapping(value = "/api/users",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public List<User> listUsers() {
...
}

Gwt communicates with Spring Rest Api and AutoBean

I have a standalone Spring Rest Api. I have models annotated with JPA.
I want to write a gwt client using this rest api.
But, I don't want to create JavaScript Overlay types for each model object type.
The interface logic on Gwt AutoBean looks good but I could not figure out how I integrate with my standalone spring application. Can you help me?
Or do you recommend any other structures to ease the process of handling rest api responses?
Yes it is possible to use AutoBean together with Spring REST API.
The serialized form of the AutoBean mirrors the interface declaration (see here for more details).
I am using AutoBean with Spring MVC REST API + Jackson serialzier and it works without any problems (at list for simple beans).
Spring MVC Controller:
#RequestMapping(method = RequestMethod.GET,value="/REST/{id}/data")
public #ResponseBody
MyDTO getData(#PathVariable("id") Long id) {
MyDTO data = null;
// retrieve data
return data;
}
GWT client side:
AutoBeanFactory:
public interface MyFactory extends AutoBeanFactory {
AutoBean<MyDtoAutobean> data();
}
Retrieve AutoBean:
MyDtoAutoBean data = AutoBeanCodex.decode(factory,MyDtoAutoBean.class,responseText).as();
responseText is the body of your GET request to your REST API.
MyDTO is a class on the server side and MyDtoAutoBean is the corresponding interface on the client (GWT) side.
They don't have to be the same. However the getters should match otherwise you have to use #PropertyNameto change the mappping.

Spring JSON+RestFul

I have a project that has ability of Core Spring and I want to add RestFul Ability,
Actually I want to send and Receive JSON data through Spring Web MVC or Whatelse others.
Imagine that,
I have a Hibernate Entity names is category.
I want to send this Entity as Json Data with Spring and
I want to send List as Json Data,also .Naturally, I want to take this Json data
from someWhere with Spring .
How can I do this Ability with spring ?
Are there any tutorials?
You will have to have jackson in your Classpath for Spring to render the response as json.
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-json-mapping
Spring 3.0 making JSON response using jackson message converter
In addition you also need to make sure that you have the following line in your Spring XML:
<context:annotation-config />
Otherwise jackson will not be enabled.
As the result, all methods which are mapped by #ResponseBody and returns object will be serialized and returned as response using application/json content type.