Spring JSON+RestFul - json

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.

Related

Spring Boot to return JSON String from an external API

I have a simple Spring boot project that uses controller mappings to get hard coded information from a class in my project.
For example, if I run the request : localhost:8080/topics, A JSON response is returned with the list of Topic Objects that i have previously created
I want to take this one step further and have a class who's variables are populated by calling this API and parsing the response : https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo
I believe this can be done in Java by creating a HTTP connection and reading the data from an input stream, but is the an easier way of doing this with spring boot? Im not fully sure of the name of this procedure hence Im having trouble finding solutions online
Since you are using Spring Boot, making use of Spring's RestTemplate makes sense. It comes with several message converters out of the box, and uses Jackson by default for json content.
Spring has published a good Getting Started page for consuming RESTful web services.
However, the json content returned by that services doesn't look like it will map well to a Java object, so you may have to deserialize it to a HashMap to get to the data you want.
I did an attempt to create something like this.
https://github.com/StanislavLapitsky/SpringSOAProxy
The idea is to register controller interfaces. Each of the interfaces are mapped to some URL. For the interfaces a dynamic proxy are generated (if the implementations are not available locally). So developer just call controller's interface method. The method is invoked for dynamically generated proxy. The proxy uses RestTemplate to call remote URL. It sends and receive JSON and deserializes the returned JSOn to POJO objects returned from the controller.
You need to declare contract - controller interfaces plus DTO to exchange data as well as mapping to understand which URL should be called for each controller.

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

Spring MVC Jackson Json #JsonIgnore programmatically

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

JSON issue in Spring 3 and

I am using Spring 3.0 and ExtJS. I have been trying to send a Map object from my controller to jsp. When I putting a pojo in HashMap and sending that HashMap to view.
From controller it is returning a Map but in ExtJS it is not able to read the response and gives below error.
HTTP Error code: 406
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
Can anyone tell that how this can be resolved?
I dont think you can just shove any POJO into a map and return it via an HttpResponse. That's not how JSON works.
In order to send JSON from Java, you have to do the equivalent of serializing it using a JSON API (or roll your own). FlexJSON is one I use, as it ships in Spring Roo and is pretty easy.

How to configure Json Request and Response with Spring

I want to configure my application to send and receive Json objects.I searched on the net I found different solution but whinch one is the best or easy to implement.Things,I want,are to send Object For example ,Category hibernate entity,I want to send List as Json output or , I want to get Category class as Json Object and I'll save it database in my controller function.
You have to implement a message converter in Spring.
Refer to http://static.springsource.org/spring/docs/current/spring-framework-reference/html/remoting.html#rest-message-conversion (JSON messageconverter already exists in Spring 3.0.5 release)