Reputation: 1278
I have this code
spring-context.xml
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
JsonResponse.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.globe.egg.ggrocery.item.model;
/**
*
* @author Farhan Sharief
*/
public class JsonResponse {
private int status;
private Object body;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Object getBody() {
return body;
}
public void setBody(Object body) {
this.body = body;
}
public static JsonResponse createResponse(int status, Object body) {
JsonResponse response = new JsonResponse();
response.setStatus(status);
response.setBody(body);
return response;
}
}
and my controller
@RequestMapping(value = "/list",
method = RequestMethod.GET)
public @ResponseBody JsonResponse getCategories() {
return JsonResponse.createResponse(SUCCESS.getStatus(), categoryService.getCategories()); //returns a List<Category>
}
I'm expecting
{
"status": 200,
"body": [
{
"categoryId": 1,
"categoryName": "basket",
"subCategory": "NONE",
"version": 1
},
{
"categoryId": 2,
"categoryName": "Cosmetics",
"subCategory": "NONE",
"version": 1
}
]
}
but i'm getting
{
status: 200
body: [2]
0: {
categoryId: 1
categoryName: "basket"
subCategory: "NONE"
version: 1
}
1: {
categoryId: 2
categoryName: "Cosmetics"
subCategory: "NONE"
version: 1
}
}
my problem is that the json has no double quotes and structure of the body is not valid. It has [2] in it.
Upvotes: 0
Views: 238
Reputation: 1278
It turned out it's because of the view of my advanced rest client.
Upvotes: 1