In my spring MVC 4.1.5 application configured to use Thymeleaf 2.1.4 (before it was using JSP and it worked fine), i am unable to return a JSON response.
It always returns a full HTML page weather my request mapping is in a #RestController or if its annotated with #responsebody
Here are the controllers
in an #controller class, i have below mapping
#RequestMapping(value = { "/", "/land", "/login" }, method = RequestMethod.GET)
public String getLogin(Model model, HttpSession session) {
session.setAttribute("login", "none");
System.out.println(programId);
model.addAttribute("signUpForm", new SignUpForm());
return "login";
}
and in #RestController class, below is the post method for same URL
#RequestMapping(value = {"/login" }, method = RequestMethod.POST )
public #ResponseBody HashMap<String, Object> login2(#RequestBody SignUpForm signUpForm, HttpServletRequest request,
HttpServletResponse httpServletResponse, HashMap<String, Object> mo, HttpSession session ) {
User user = userDao.findUserByName(signUpForm.getUserName());
if (user != null && encoder.matches(signUpForm.getPassword(), user.getPassword())&& user.getProgram_id()==3) {/* && user.getProgram_id()==3*/
session.setMaxInactiveInterval(1200);
System.out.println(session.getMaxInactiveInterval()+":"+session.getLastAccessedTime()+":"+session.getCreationTime()+":"+session.getServletContext().getContextPath());
session.setAttribute("login", "success");
mo.put("redirect", "/home");
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
session.setAttribute("urip", ipAddress);
return mo;
} else {
mo.put("error", "Login failed. Please check your credentials");
return mo;
}
}
Below is my xml configuration
<context:component-scan base-package="com.company.cardholder" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="com.company.cardholder.session.interceptors.URLInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/thymeleaf/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<!-- Template cache is set to false (default is true). -->
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
Here is my JSON call
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: JSON.stringify({
userName: $form.find('#userName').val(),
password: $form.find('#password').val(),
}),
contentType: "application/json",
/*dataType: 'json',*/
complete: function(data) {
console.log(data);
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
}else if(data.error){
$messageError.text(data.error);
$messageError.removeClass('hidden');
$messageSuccess.addClass('hidden');
}
}
});
Ok. Here is what i figured out to make it work but i still am not sure of the reason.
public #ResponseBody HashMap<String, Object> login2(#RequestBody SignUpForm signUpForm, HttpServletRequest request,
HttpServletResponse httpServletResponse, HashMap<String, Object> mo, HttpSession session ){
////
}
In the above method signature, i was "injecting" a hashmap. And spring framework default or some unknown config decided to inject a "Binding Result aware Hashmap." I am not sure what difference it would have made. But to fix it, i had to do a
HashMap<String, Object> mo=new HashMap<String, Object>;
inside the method body and remove the injected hashmap.
If anyone is able to understand this behaviour, please explain it. I feel i missed something basic in my awareness of spring framework.
Related
I am trying to configure my Spring application to return either JSON or CSV based on user requested media Type. In order to do that I have set up my tag like below
<context:annotation-config />
<bean id ="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="true"/>
<property name="parameterName" value="mediaType"/>
<property name="defaultContentType" value="test/csv"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="csv" value="test/csv"/>
</map>
</property>
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<bean class="com.data.api.util.CSVMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="modules" ref="jodaModule"/>
<property name="featuresToDisable">
<array>
<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
</array>
</property>
<property name="defaultViewInclusion" value="false"/>
<property name="failOnUnknownProperties" value="false"/>
</bean>
</property>
<property name="supportedMediaTypes">
<array>
<util:constant static-field="org.springframework.http.MediaType.ALL" />
</array>
</property>
</bean>
<bean id="jodaModule" class="com.fasterxml.jackson.datatype.joda.JodaModule"/>
</mvc:message-converters>
</mvc:annotation-driven>
Below is my CSVMessageConverter class
public class CSVMessageConverter extends AbstractHttpMessageConverter {
public static final MediaType MEDIA_TYPE = new MediaType("text", "csv", Charset.forName("utf-8"));
public CSVMessageConverter() {
super(MEDIA_TYPE);
}
#Override
protected boolean supports(Class<?> clazz) {
return true;
}
#Override
protected CSVMessage readInternal(Class<? extends CSVMessage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
CSVReader reader = new CSVReader(new InputStreamReader(inputMessage.getBody()));
return new CSVMessage(reader.readAll());
}
#Override
protected void writeInternal(CSVMessage message, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
CSVWriter writer = new CSVWriter(new OutputStreamWriter(outputMessage.getBody()));
if (! Strings.isNullOrEmpty(message.getHeader())) {
writer.writeNext(new String[] { "#" + message.getHeader() } );
}
for (String[] row: message.getRows()) {
writer.writeNext(row);
}
writer.close();
}
}
and My Controller does this
public class TestController {
public #ResponseBody List<Records> getHits(#PathVariable("domain") name domainName,
#PathVariable("property") Name propertyName,
#RequestParam(value = "start", required = true) #DateTimeFormat(pattern = datePattern) Date startDate,
#RequestParam(value = "end", required = true) #DateTimeFormat(pattern = datePattern) Date endDate,
HttpServletRequest request) {
//This returns an arralist of records
}
}
I am unable to get my CSVMessageConverter to work. It is throwing cannot cast ArrayList to CSVMessage. What am I doing wrong ? It works fine with JSON but If i request for "text/csv" it blows up.
Can someone explain how Spring uses MessageConverters and WhatI can do to make the above code work.
I fixed this by fixing my CSVMessageConverter class. It was expecting CSVMessage class in readInternal/WriteExternal. I changed this to ArrayList.class and these methods get called fine.
I have a Spring MVC Rest service that return JSON value, i have 8 row, here is my JSON
[
{"no":"1","date":"2015-03-30","grandtotal":699618,"diskon":699618},
{"no":"2","date":"2015-03-30","grandtotal":1867949,"diskon":1867949},
{"no":"3","date":"2015-03-27","grandtotal":2190909,"diskon":2190909},
{"no":"4","date":"2015-03-26","grandtotal":8616120,"diskon":8616120},
{"no":"5","date":"2015-03-26","grandtotal":1095455,"diskon":1095455},
{"no":"6","date":"2015-03-24","grandtotal":938961,"diskon":938961},
{"no":"7","date":"2015-03-24","grandtotal":5603848,"diskon":5603848},
{"no":"8","date":"2015-03-20","grandtotal":3735899,"diskon":3735899}
]
What i trying.. Here is my controller. SpringRestController.java
Jackson Way :
#RequestMapping(value = "/view", method = RequestMethod.GET)
public String initCreationForm(Map<String, Object> model) {
String url = "http://localhost:8080/SpringServiceJson/view/";
RestTemplate restTemplate = new RestTemplate();
TotalDiscList totaldisc = restTemplate.getForObject(url, TotalDisc.class);
model.put("DiscValue",totaldisc);
return "salesorders/totalDisc";
}
Gson way :
public String initCreationForm(Map<String, Object> model) {
String url = "http://localhost:8080/SpringServiceJson/view/";
Gson gson = new Gson();
Collection<TotalDisc> totaldisc = gson.fromJson(url, PiutangListJson.class);
model.put("DiscValue",totaldisc);
return "salesorders/totalDisc";
}
what i missed here? it always give me this error
"Could not extract response: no suitable HttpMessageConverter found for response type [class [Lorg.springframework.samples.my.model.TotalDiscList;] and content type [application/json]"
Here is my object TotalDiscList.java
public class TotalDiscList {
private String no;
#DateTimeFormat(pattern="dd-MM-yyyy")
private Date date;
private long grandtotal;
private long diskon;
//getter setter skipped
}
i should return List<TotalDiscList> totaldisc = restTemplate.getForObject(url, List<TotalDisc>.class); right?
how i do that properly?
If you have a servlet-context.xml, you can add the message-convertor there, like below :
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters" ref="jsonMessageConverter" />
</beans:bean>
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
I am doing the same and it works.
Edit
You need to provide a Message converter to your RestTemplate
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
And then try using an array, such as:
TotalDiscList[] totaldisc = restTemplate
.getForObject(url, TotalDiscList[].class);
you can configure a json message converter:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
and then, you can just annotate your method:
#RequestMapping(value = "/view", method = RequestMethod.POST)
public TotalDiscList[] createDiscList(
#RequestBody TotalDiscList[] object) {
}
which would cause the message converter to try to convert to the given class.
If I use the following configuration then jackson converter works (mvc declaration is last)
<!-- Configure to plugin JSON as request and response in method handler -->
<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">
</bean>
<context:component-scan base-package="com.base" />
<mvc:annotation-driven />
If I use this configuration in dispatcher.xml then validation works but conversion does not. (mvc declaration first)
<context:component-scan base-package="com.base" />
<mvc:annotation-driven />
<!-- Configure to plugin JSON as request and response in method handler -->
<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">
</bean>
Any help greatly appreciated.
Spring version 4.0.6
I chose the part where validation is working and added this in the code base.
#RequestMapping(value = "url", method = RequestMethod.GET)
protected void getLocationAsJson(#PathVariable("id") Integer id,
#RequestParam("cid") Integer cid, HttpServletResponse response) {
MappingJacksonHttpMessageConverter jsonConverter =
new MappingJacksonHttpMessageConverter();
Location loc= new Location(id);
MediaType jsonMimeType = MediaType.APPLICATION_JSON;
if (jsonConverter.canWrite(requestedLocation.getClass(), jsonMimeType)) {
try {
jsonConverter.write(requestedLocation, jsonMimeType,
new ServletServerHttpResponse(response));
} catch (IOException m_Ioe) {
// TODO: announce this exception somehow
} catch (HttpMessageNotWritableException p_Nwe) {
// TODO: announce this exception somehow
}
}
}
Now the validation works as well as JSON returning.
The method is not returning anything.
RequestMappingHandlerAdapter's xml configuration is bit complicated. The problem with this configuration is, it removes spring default configuration for converters. It is better to use coding version of this configuration. Spring default configuration will be intact this way. Here is sample configurations.
Suggested solution, posted on numerous blogs. But not working in my case.
https://dzone.com/articles/customizing
http://www.java-allandsundry.com/2014/09/customizing-httpmessageconverters-with.html
#Configuration
public class MessageConvertorConfiguration extends WebMvcConfigurationSupport {
#Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
Custom360DateFormat dateFormat = new Custom360DateFormat();
dateFormat.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
dateFormat.setDateTimeFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm a"));
objectMapper.setDateFormat(dateFormat);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
}
Working Solution
#Configuration
public class MessageConvertorConfiguration {
private MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
Custom360DateFormat dateFormat = new Custom360DateFormat();
dateFormat.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
dateFormat.setDateTimeFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm a"));
objectMapper.setDateFormat(dateFormat);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
#Autowired
public void updateJacksonConvertor(RequestMappingHandlerAdapter handlerAdapter) {
//remove default jakson convertor
Iterator<HttpMessageConverter<?>> convertorsIterator = handlerAdapter.getMessageConverters().iterator();
while (convertorsIterator.hasNext()) {
HttpMessageConverter converter = convertorsIterator.next();
if(converter instanceof AbstractJackson2HttpMessageConverter) {
convertorsIterator.remove();
}
}
handlerAdapter.getMessageConverters().add(customJackson2HttpMessageConverter());
}
}
I'm getting an error that the request sent by the client was syntactically incorrect. What is being done wrong? Here is my code:
#Entity
#Table(name = "display")
public class Display {
private String diagonal;
private String aspectRatio;
//getter and setter
}
$.ajax({
type:'POST',
url:'/admin/updateDisplay',
data:{'diagonal':"sss"}
})
#Controller
#RequestMapping(value = "/admin")
public class AdminController {
#RequestMapping(value = "/updateDisplay", method = RequestMethod.POST)
public String updateDisplay(#RequestBody Display display){
System.out.print(display);
return null;
}
}
I think you need to say what the service media type will consume for Spring to know how to unmarshall it. Probably application/json.
#RequestMapping(value = "/updateDisplay", method = {RequestMethod.POST},
consumes = {"application/json"})
Probably some Json library too, like Jackson.
Use the following:
$.ajax({
type:'POST',
url:'/admin/updateDisplay',
data:{"diagonal":"sss","aspectRatio":"0.5"},
contentType: 'application/json',
dataType: 'json',
})
it works.
EDIT
If you are booting up Spring application Context using annotaitons, then your config class must have:
#Override
protected void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).favorParameter(true)
.parameterName("mediaType").ignoreAcceptHeader(true)
.useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
And your ajax request must include
contentType: 'application/json',
dataType: 'json',
check the modified ajax call above.
If you are booting up spring application context using XMLs then use the below:
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="mediaType" />
<property name="ignoreAcceptHeader" value="true"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
For more details on writing RESTFUL webservices with Spring 3.2 see my blog
You must convert the JSON data to string before pass it to Spring MVC. So, here is the solution in your case:
$.ajax({
type:'POST',
url:'/admin/updateDisplay',
data: JSON.stringify({'diagonal':"sss"})
})
You don't need #RequestBody.
With #RequestBody Spring calls a converter that takes the whole request and converts it to an object of the required type. You send your data as application/x-www-form-urlencoded, which is the default of jQuery, and there is no built-in converter for that.
Without #RequestBody, when you send form data, spring creates an empty object and sets the properties based on the data you sent. So in your case Spring would do something like
display = new Display();
display.setDiagonal("sss");
Which, I guess, is what you want.
I don't know if this is your problem too, but with me the value is wrong and caused a error 405, example:
#RequestMapping(value = "/planilha/{id}", method = RequestMethod.PUT)
public String update(#PathVariable("id") String id, #RequestBody String jsonStr) {
BasicDBObject json = ((BasicDBObject) JSON.parse(jsonStr));
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject ola = dao.update(id, json);
return ola.toString();
}
#RequestMapping(value = "/planilha/{id}", method = RequestMethod.DELETE)
public String delete(#PathVariable("id") String id) {
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject temp = dao.remove(id);
return temp.toString();
}
Needed the change for:
#RequestMapping(value = "/planilha/{id}/**", method = RequestMethod.PUT)
public String update(#PathVariable("id") String id, #RequestBody String jsonStr) {
BasicDBObject json = ((BasicDBObject) JSON.parse(jsonStr));
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject ola = dao.update(id, json);
return ola.toString();
}
#RequestMapping(value = "/planilha/{id}", method = RequestMethod.DELETE)
public String delete(#PathVariable("id") String id) {
PlanilhaDAO dao = new PlanilhaDAO();
BasicDBObject temp = dao.remove(id);
return temp.toString();
}
I am using Spring REST framework and Jackson JSON
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="customObjectMapper" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="customObjectMapper"class="com.rackspace.payment.webapp.utils.JaxbJacksonObjectMapper" />
public class JaxbJacksonObjectMapper extends ObjectMapper {
#SuppressWarnings("deprecation")
public JaxbJacksonObjectMapper() {
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
super.getDeserializationConfig().withAnnotationIntrospector(introspector);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
super.getSerializationConfig().withAnnotationIntrospector(introspector);
}
}
//JXAB element
#XmlAttribute(name = "createDate")
#XmlJavaTypeAdapter(CustomDateAdapter.class)
protected Date createDate;
The issue I face is, when the JSON output is displayed 'createDate' element is displayed like a string the xml adapter class is not invoked, whereas it works fine in XML output.
What am I missing? how to invoke the XmlJavaTypeAdapter in JSON format.
the code below did the trick for me .
ObjectMapper objectMapper = new CustomObjectMapper(ENTITY_WITH_ID_DESERIALIZER);
AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
objectMapper.setAnnotationIntrospector(pair);
objectMapper.configure(DeserializationConfig.Feature.USE_ANNOTATIONS, true);