Converting array of Object to JSON and send it over HTTP - java

I have an array of object as ArrayList which I want to convert to JSON and send it to client over HttpResponse using Java and Jackson.
How I can do this?

Assuming you already put jackson library and using Spring framework. Mentioned following line in your spring context file.
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
and Return the List/Map/Object from controller and it will internally convert it to JSON
For Non Spring:
doPost(HttpServletRequest req, HttpServletResponse resp)
{
ObjectMapper mapper = new ObjectMapper();
ArrayNode rootNode = mapper.readValue(req.getReader(), ArrayNode.class);
}
Source:
Passing array from javascript to java servlet using Jackson
http://software.danielwatrous.com/restful-java-servlet-serializing-tofrom-json-with-jackson/
Hope this helps :)
Do share your environment details if it was still not helpful and i will try to help :)

Related

How to handle 400 error in Spring MVC

I am getting 400 Http response when i am passing the invalid json format,
I would like to return the custom json message instead of this , can any one advise how to do in Spring 4.1 ?
Handling Execption using ControllerAdvice,but it is not working.
#ControllerAdvice
public class GlobalControllerExceptionHandler {
#ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class})
#ResponseStatus(HttpStatus.BAD_REQUEST)
#ResponseBody
public String resolveException() {
return "error";
}
}
spring-config.xml is given below
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- Renders JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
Given below Json request and response from WebSphere application server (7.0).
Request 1: Empty json request : {}
Response Status Code: 400 Bad Request
Response Message : Json request contains invalid data:null
Request 2:Invalid format of Json Request : {"data":,"name":"java"}
Response Status Code: 400 Bad Request
Response or Exception message :
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character (',' (code 44)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: com.ibm.ws.webcontainer.srt.http.HttpInputStream#8f308f3; line: 5, column: 57]
Similar question like below link
Using Spring MVC, accepting POST requests with bad JSON leads to a default 400 error code server page being returned
You can attempt to map the exception this way. This code will return a 400 status, but you can change the return the same way as is the link you posted
#ExceptionHandler
#ResponseStatus(HttpStatus.BAD_REQUEST)
public void handleJsonMappingException(JsonMappingException ex) {}
Finally i have handle the exception via Servlet Filter with HttpServletRequestWrapper.
Step 1: Add the filter
Step 2: Get the request body from Customize HttpServletRequestWrapper class
Step 3: Convert request body json string to java object using JSON API
Step 4: Chain the request/response
Step 5: Catch exception / and update the HttpServlet Response
Using below reference.
Filter Example
HttpServletRequestWrapper Example
String to Json Object
With the help of this approach i can handle 400/405/415 Http Errors.
You may try this, in your pom.xml add dependency:
<!-- Need this for json to/from object -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
this will convert your java objects to JSON automatically when you return them. like you can write a class for response:
public class Response {
private int responseCode;
private String responseMessage;
//as many fields as you like
public Response (int responseCode, String responseMessage) {
this.responseCode = responseCode;
this.responseMessage = responseMessage;
} }
then you can return any java objects and they will be received as JSON,
#RequestMapping(value="/someMethod", method=RequestMethod.POST)
public #ResponseBody Response someMethod(#RequestBody Parameters param) {
return new Response(404, "your error message");
}

Spring RabbitMQ Messaging. Using JSON send efficiently

If using JSON Object to send and receive over RabbitMQ is this the cleanest way to send and receive? All that conversion seems inefficient.
Sending Code
JSONObject messageJSON = new JSONObject();
messageJSON.put("messageId", "testId");
messageJSON.put("NodeId", "testNode");
template.convertAndSend("TEST-EXCHANGE",
"routing.test", messageJSON.toJSONString()
.getBytes());
Receive Code
public class Listener implements MessageListener {
#Override
public void onMessage(Message message) {
String recmessage = new String(message.getBody());
JSONObject obj = (JSONObject) JSONValue.parse(recmessage);
System.out
.println("Message Received " + (String) obj.get("messageId"));
}
}
Solution From Answer given
You need to add the Dependency of Jackson. Below is maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
Add to the Spring Config
<bean id="amqpCorrectionTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageConverter">
<bean
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">
</bean>
</property>
</bean>
Add to the message Listener
Jackson2JsonMessageConverter jmc = new Jackson2JsonMessageConverter();
JSONObject obj = (JSONObject) jmc.fromMessage(message);
Sending Code
Only send the JSON Object passed in.
template.convertAndSend("TEST-EXCHANGE",
"routing.test", messageJSON);
This wrong question: the AMQP protocol (as any other wire-based) gets deal just with bytes, so it's up to your application how to convert to byte[] and back (if it is Java everywhere, of course).
From other side it would be better to your configuration based on the out-of-the- box converters. For example Jackson2JsonMessageConverter.
See more information in the Documentation.

Spring RestTemplate unable to parse json response

If I parse the response of a POST as string it works perfectly:
ResponseEntity<String> stringResponse = restTemplate.postForEntity(DruidClient.QUERY_HOST + "/druid/v2", query, String.class);
String valueResults = stringResponse.getBody();
DruidValueResult[] results = new ObjectMapper().readValue(valueResults, DruidValueResult[].class);
However, if i tell spring to parse the response directly:
ResponseEntity<DruidValueResult[]> results = restTemplate.postForEntity(DruidClient.QUERY_HOST + "/druid/v2", query, DruidValueResult[].class);
I get the following error:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class [Lcom.dripstat.metricprocessor.druid.DruidValueResult;] and content type [application/smile]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:788)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:773)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:553)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:361)
Why isn't spring able to parse the resulting json directly?
From SpringSource Blog:
Objects passed to and returned from the methods getForObject(), postForLocation(), and put() and are converted to HTTP requests and from HTTP responses by HttpMessageConverters. Converters for the main mime types and Java types are registered by default, but you can also write your own converter and plug it in the RestTemplate. In the example below, I will show you how that's done.
I suppose the same for postForEntity(), so you may need to add a message converter for your specific mime type since it is not marshelled by default:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/smile" />
<property name="supportedMediaTypes" value="text/javascript" />
</bean>
</list>
</property>
</bean>

Use different viewresolver depending on request

I try to solve the following problem:
I have set up my own viewResolver, adding a header and footer to all my requests (thanks, stackoverflow ;p). This works fine.
Problem is that my ajax-requests, that return a view to a specific container, also automatically get the header and footer which of course is not intended.
How can I get the viewResolver to act different if a request contains '/ajax/'? Actually, the jstl-Viewer works fine for them, but at the moment, my own resolver is the only one used because I don't know how to tell spring to act different on '/ajax/'.
Here my own resolver, pretty simple, just taken from here:
public class ViewTemplate extends InternalResourceView {
#Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
String dispatcherPath = prepareForRendering(request, response);
request.setAttribute("partial", dispatcherPath.substring(nthOccurrence( dispatcherPath, '/' , 2 ) + 1));
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout.jsp");
rd.include(request, response);
}
// more functions like nthOccurrence
}
Do I have to tell this resolver that if indexOf("/ajax/") != -1 jstView needs to be called? And if yes, how?
Or am I doing it completely wrong? I would like to keep my header/footer as they are for all non-ajax-requests!
Any help appreciated!
Thanks!
You have put the code for a View not a ViewResolver right, I think a good solution could be to chain the ViewResolvers - just declare a new ViewResolver for your ajax views. For the Ajax related views, let this view resolver return a value, else return null, this will automatically get the ViewResolver to consider the next ViewResolver in the chain which can be your current ViewResolver.
eg.
<beans:bean id="ajaxViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="order" value="1"></beans:property>
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="viewNames">
<beans:list>
<beans:value>ajax*</beans:value>
</beans:list>
</beans:property>
<beans:property name="suffix" value=".jsp" />
</beans:bean>
In the above case, if you return your view name from a controller as "ajaxsomeview" then it will be handled by ajaxViewResolver, if you return "somethingelse" it will be handled by your current ViewResolver.

How to get a MultipartHttpServletRequest from RequestContextHolder?

I have configured the access decision manager to check a request before being processed by the servlet the key line is:-
HttpServletRequest request = (HttpServletRequest) RequestContextHolder.currentRequestAttributes().getRequest();
All good. However when the request is enctype="multipart/form-data" how do I get hold of the MultipartHttpServletRequest when RequestContextHolder.currentRequestAttributes().getRequest() only returns HttpServletRequest?
I am using spring 2.5.
MultipartHttpServletRequest is n Spring-specific interface for handling multipart form submissions. The default implementation is DefaultMultipartHttpServletRequest, which has a constructor that takes a HttpServletRequest.
So:
HttpServletRequest originalRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
MultipartHttpServletRequest multiPartRequest = new DefaultMultipartHttpServletRequest(originalRequest);
Apart from having
<form method=<method> action=<url> enctype="multipart/form-data"></form>
you have to have
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
in your spring configuration file.
Here is nice tutorial on the same
http://techdive.in/spring/spring-file-upload
Have you tried casting to MultipartHttpServletRequest?
If you are using spring-mvc, make sure you put this line
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
in your app-config.xml.
This worked for me.
I don't think you can get DefaultMultipartHttpServletRequest from RequestContextHolder.
DefaultMultipartHttpServletRequest really implements HttpServletRequest.
But there're 2 request instances if you use CommonsMultipartResolver. One is DefaultMultipartHttpServletRequest instance, and another is HttpServletRequest instance.
Actually I don't know how to get the first instance from RequestContextHolder. You can get the second instance from it.

Categories

Resources