Spring MVC Jackson Json #JsonIgnore programmatically - json

I have a rest web service written in spring mvc and using jackson json for the output. To ignore an object while serialising to json, I know I can annotate the field or class with #Jsonignore. But I have an array of objects the I want to return and whether to ignore some objects from the final json output depends on some condition. For example: ignore if the object.hasTitle() == null.
What would be the best way to achieve this in spring mvc?

One solution whould be to provide a custom (de)serializer where you can remove/add fields on specific conditions.
Right way to write JSON deserializer in Spring or extend it

Related

Efficient way of parsing Jax-ws Restful Response

I need to parse the jax-ws rest response and I tried the following two ways of parsing the response.Both works good.But I am in need to know the best efficient way of implementation.Please provide me your view.
First Approach:
Use getEntity Object and get the response as Input Stream.
Using Jackson ObjectMapper readValue() -covert the inputstream to java
object.
Using getters and setters of nested java class get the response objects member values.
Second Approach:
Use getEntity Object and get the response as Input Stream and and
convert the Input Stream to String.
Using Google Json API,convert the string to json object.
Using Json parser and get the nested objects member values.
I would say the first approach is better for two reasons:
You don't go through the intermediate process of reading the response payload into String
The setter methods that are called during Jackson deserialization may perform validation on input and throw appropriate exceptions, so you do validation during deserialization.
Maybe not a general answer to this question but another variant of what you're describing under "First approach". I would start with a generic data structure and would only introduce an extra bean if necessary. I wouldn't use String to pass structured data around.
Use jackson to convert the JSON response to a
Map<String,Object> or JsonNode.
Advantage:
You don't need to implement a specialized bean class. Even a very simple bean can become unhandy over time (if format changes or new nested structures are added to the json response, etc.). It also introduces some kind of metaphor to your code which sometimes helps but also can be misleading.
Map<String,Object> is in the JDK and offers a good interface to access data. You don't have to change any interfaces even if the JSON format changes.
You can always pass your data in form of a Map<String,Object>
Disadvantage
Data Encapsulation. The map is a very close representation of the input data and therefore offers not same level of abstraction like a bean.

Suggestion required: RESTFUl webservice transform xml to Json

Hi I am new to Java and not sure how to proceed (kindly ignore any typos or my language). Can somebody help me out (just the Idea/how to proceed, dont need any sample code). I am trying to create a Restful Json webservice (using Spring MVC).
The webservice that I am trying to create is kind of a wrapper for an existing XML based restful webservice.
The Idea is to have one common platform, since all other existing services are exposed as as JSON services.
My job is to fetch the XML transform it into a Json, but the tricky part is The Json schema is a superset of the XML schema (I mean it contains more elements that get filled with some default values).
Please let me know if you need more info.
Thanks in advance.
One way to do it would be to use Jaxb to transform the the incoming XML to Java Objects. Build your Jaxb objects in a way that it contains all the elements, the one with default values and the elements in the incoming XML.
Ones the XMl is converted into Jaxb you can use org.springframework.http.converter.json.MappingJacksonHttpMessageConverter message converter to convert your Jaxb object to Json string.

How to enrich a spring controller json's response

I've got a simple Spring MVC app where controllers are generating Json objects and returning them as Strings.
I'd like to return those json trees as-is out of the controllers and have a kind of servlet filter which will enrich them. Basically it will take the json node and move it as a child of a brand new json root. Think of a 'body' encapsulated in a complete response including also a 'head' child node that would be generated by this filter.
It is possible to do so within Spring ?
Thanks for your advices !
I would advise instead of returning plain Strings, return POJO and convert this to JSON using HTTP Message converters (e.g. MappingJackson2HttpMessageConverter) and #ResponseBody annotation. Then you can decorate the return object the way you want.

Configure Jackson Mapper from Xml

I need to generate Json from the domain object. I can't add annotation in the domain classes.
Using Mixin is not a option because I have to ignore a lot of properties.
My approach was create a DTO object with the properties that I need. And populate the dto using dozer and then generate Json from the dto with jackson. It looks too much.
I would like to know if is posible to configure Jackson from xml, in order to generate json with the properties mapped in the xml, so it would not be necessary to use dto and dozer.
No. Jackson does not support external configuration files.
But you don't explain how or why you would use Dozer, or DTO. Why not just add properties you care about in a Map, and serialize that as JSON? Then you can use whatever mechanism you want to build/trim that Map.
Jackson can also convert values, so to create full Map with everything from another object, you can do:
Map<String,Object> map = objectMapper.convertValue(someBean, Map.class);
and maybe then only retain properties you want.

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.