json request not parsed in controller correctly - json

i am struggling with something that should work but doesn't...
i have this mapping in my controller:
#RequestMapping(value = "/keys", method = RequestMethod.POST)
#Consumes(MediaType.APPLICATION_JSON)
public ResponseEntity<Void> parseKeyList(keyList keyList) {
return new ResponseEntity<Void>(HttpStatus.OK);
}
with the simple class
#XmlRootElement
public class keyList {
private String keys;
public String getKeys() {
return keys;
}
public void setKeys(String keys) {
this.keys = keys;
}
}
And I am sending this simple JSON post:
{"keys": "This is my key list"}
but I am getting null in the keys.
As requested the original dispatcher-servlet:
<context:component-scan base-package="com.api" />
<!-- <mvc:resources mapping="/*" location="/WEB-INF/pages/" /> -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

You haven't attached your keyList param to the request body, change the method to:
#RequestMapping(value = "/keys", method = RequestMethod.POST)
#Consumes(MediaType.APPLICATION_JSON)
public ResponseEntity<Void> parseKeyList(#RequestBody keyList keyList) {
return new ResponseEntity<Void>(HttpStatus.OK);
}
Btw.: Class keyList should rather be KeyList (with big K).
Also there is (look at the chat) a
<mvc:annotation-driven/>
missing from your dispatcher-servlet.xml. That's the one that is registering the jackson mapper/marshaller.

Related

Hibernate #OneToOne gets a NullPointer Exception

I am reviewing my Java skills and your support to the community is amazing, your tips have helped me a lot.
I am stuck in a Hibernate #OneToOne configuration, it is a very simple design and code but I can´t find the error. I´d really appreciate your help.
This is the user.java code, the user_id is generated by an autoincrement column at MySQL. I have omitted hash() and equals() code for simplicity. Hibernate understands the User class but something is missing to get to the Address class that is at the other side of the relationship.
Any help would be very appreciated.
Thank you
1) This is the user.java:
package myPackage;
import java.io.Serializable;
import java.util.Arrays;
import javax.persistence.*;
#Entity
#Table(name="user1")
public class User implements Serializable {
private static final long serialVersionUID = 3271213543123246487L;
#Id
#GeneratedValue
#Column(name="user_id")
private Integer user_id;
#Column(name="user_name", length=100, nullable=false)
private String user_name;
#OneToOne (cascade= CascadeType.ALL)
#PrimaryKeyJoinColumn(name="user_id")
private Address myAddress;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Address getMyAddress() {
return myAddress;
}
public void setMyAddress(Address myAddress) {
this.myAddress = myAddress;
}
}
2) This is the Address.java code:
package myPackage;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.Parameter;
#Entity
#Table(name="address1")
public class Address implements Serializable {
private static final long serialVersionUID = 3605176021936036836L;
#Id
#GeneratedValue(generator="address_ibfk_2")
#org.hibernate.annotations.GenericGenerator(name="address_ibfk_2",
strategy="foreign",parameters =#Parameter(name="property",value="user1"))
#Column(name="user_id")
private Integer user_id;
#Column(name="address_line1", length=100, nullable=false)
private String address_line1;
public String getAddress_line1() {
return address_line1;
}
public void setAddress_line1(String address_line1) {
this.address_line1 = address_line1;
}
}
3) This is the very simple TestUser class
public class TestUser {
public static void main(String[] args) {
Session mySession = HibernateUtil.getSessionFactory().openSession();
System.out.println("Connection status"+mySession.isConnected());
System.out.println("Session status"+mySession.isOpen());
Transaction myTransaction = mySession.beginTransaction();
try {
User myUser = new User();
Address myAddress = new Address();
myUser.setUser_name("TesteO2O");
myAddress.setAddress_line1("Rua A");
myUser.setMyAddress(myAddress);
mySession.save(myUser);
myTransaction.commit();
System.out.println("myUser saved sucessfully");
} catch (Exception e) {
e.printStackTrace();
myTransaction.rollback();
} finally {
mySession.close();
}
}
}
4) This is the hibernate.cfg.xml config file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--MySQL Config-->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/p2pl_dev?serverTimezone=UTC</property>
<property name="connection.username">xyz</property>
<property name="connection.password">blabla</property>
<property name="current_session_context_class">thread</property>
<!--Connection Pool Config: max_statements cached, idle time in seconds-->
<property name="c3po.min_size">2</property>
<property name="c3po.max_size">3</property>
<property name="c3po.timeout">300</property>
<property name="c3po.max_stamentes">50</property>
<property name="c3po.idle_test_period">3000</property>
<!--Debug Config, show_sql=console, format_sql=legible -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="generate_statistics">true</property>
<property name="use_sql_comments">true</property>
<!-- Classes -->
<mapping class="myPackage.User"/>
<mapping class="myPackage.Address"/>
</session-factory>
</hibernate-configuration>
5) Finally the error trace
Hibernate:
/* insert myPackage.User
*/ insert
into
user1
(user_name)
values
(?)
java.lang.NullPointerException
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:650)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4736)
at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:96)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:294)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at myPackage.TestUser.main(TestUser.java:26)

Spring multiple transaction roll back

I have a requirement wherein I have to insert into 3 separate tables through 3 DAO classes.
ClassADAO
ClassBDAO
ClassCDAO
I want to have a single transaction for all the three classes so that if one insertion fails I want to roll back the complete transaction that is inserted through previous classes.
I have my configured xml as shown below
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"></property>
<property name="username" value="******"></property>
<property name="password" value="********"></property>
</bean>
In controller annotated as follows
#Transactional(rollbackFor = { Exception.class }, propagation = Propagation.REQUIRED)
But still if ClassCDAO fails, I can c insertions in my db from ClassA and ClassB
Controller:
#Controller
#Transactional(rollbackFor = { Exception.class }, propagation = Propagation.REQUIRED)
public class MyController{
#Autowired
private MyBusinessLayer mybusinessLayer;
#RequestMapping(value = "/register.htm", method = RequestMethod.POST)
public String saveRegistration(
final #ModelAttribute("registration") #Valid Registration registration,
final BindingResult result, final Model model) {
if (result.hasErrors()) {
return "myPage";
} else {
mybusinessLayer.saveRegistration(registration);
}
return "myPage";
}
}
My Business Layer:
#Component
public class MyBusinessLayer{
#Autowired
private ClassA classA;
#Autowired
private ClassB classB;
#Autowired
private ClassC classC;
public void saveRegistration(Registration registration) {
Company company = RegistrationHelper.buildCompany(registration);
classA.saveCompany(company);
Contact contact = RegistrationHelper.buildContact(registration, company.getCompanyId());
classB.saveContact(contact);
User user = RegistrationHelper.buildUser(registration, contact.getCompanyID(),
contact.getContactID());
classC.saveUser(user);
}
}
ClassADAO:
#Component
public class CompanyDAOImpl implements CompanyDAO {
#Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
#Override
public void saveCompany(Company company) {
String insertCompanySQL = //insert statement;
//code here
SqlParameterSource paramSource = new MapSqlParameterSource(bind);
namedParameterJdbcTemplate.update(insertCompanySQL, paramSource);
}
}
ClassBDAO and ClassCDAO are also as shown above.
Move your #Transactional annotation to MyBusinessLayer class. Unless you have your DataSource configured as autocommit, your code seems correct.

Getting HTTP status 400 - The request sent by the client was syntactically incorrect: using curl to post/put json request

I am working with spring MVC with both xml/json objects and I am getting following error:
HTTP Status 400 - The request sent by the client was syntactically incorrect ().
This is my controller.
#RequestMapping(method=RequestMethod.POST, value="/emp")
public #ResponseBody EmployeeList addEmp(#RequestBody Employee e) {
employeeDS.add(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}
#RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")
public #ResponseBody EmployeeList updateEmp(#RequestBody Employee e, #PathVariable String id) {
employeeDS.update(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}
I am trying to send JSON object using curl:
curl -v -X PUT -HContent-type:application/json --data '{"id":3,"name":"guest","email":"guest#ibm.com"}' http://localhost:8080/rest/service/emp/1
curl -v -X POST -HContent-type:application/json --data '{"id":3,"name":"guest","email":"guest#ibm.com"}' http://localhost:8080/rest/service/emp
I have added following jackson dependencies in pom file:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.12</version>
</dependency>
I have also added following configuration in my servlet-dispatcher.xml:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json" />
<!--entry key="html" value="text/html"/-->
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.mkyong.common.bean.Employee</value>
<value>com.mkyong.common.bean.EmployeeList</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="false" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
Here is how the Employee class looks like:
package com.mkyong.common.bean;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="employee")
public class Employee {
private long id;
private String name;
private String email;
public Employee() {}
public Employee(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
I have the following jars in my lib:
aopalliance-1.0.jar
aspectjrt-1.5.3.jar
aspectjweaver-1.5.3.jar
axiom-api-1.2.7.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
jackson-core-asl-1.9.13.jar
jackson-jaxrs-1.9.12.jar
jackson-mapper-asl-1.9.13.jar
jaxb-api-2.1.jar
jaxb-impl-2.2.jar
joda-time-1.6.2.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
slf4j-api-1.7.2.jar
spring-aop-3.1.3.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.1.3.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-oxm-3.2.2.RELEASE.jar
spring-security-config-3.1.3.RELEASE.jar
spring-security-core-3.1.3.RELEASE.jar
spring-security-web-3.1.3.RELEASE.jar
spring-tx-3.1.3.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.1.3.RELEASE.jar
spring-ws-core-2.1.2.RELEASE.jar
spring-ws-security-2.1.2.RELEASE.jar
spring-xml-2.1.2.RELEASE.jar
stax-api-1.0-2.jar
wsdl4j-1.6.1.jar
wss4j-1.6.5.jar
xmlsec-1.5.1.jar
xmlsec-2.0.jar
xmltooling-1.3.2-1.jar
xws-security-1.3.1.jar
My GET works just fine for both XML and JSON:
#RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/xml, application/json")
public #ResponseBody Employee getEmp(#PathVariable String id) {
Employee e = employeeDS.get(Long.parseLong(id));
return e;
}
for following curl command:
curl -HAccept:application/xml http://localhost:8080/rest/service/emp/1
curl -HAccept:application/json http://localhost:8080/rest/service/emp/1
I have actually created your project. Using eclipse in windows on tomcat. I used spring 3.2.4 (but I don't think it makes a difference).
I encountered the same problem as you when I sent it incorrect json so I think it is your curl command which is wrong. Are you on windows? If so you must escape your quotes. I sent it the following:
C:\> curl -v -X POST -HContent-type:application/json -d "{\"id\":3,\"name\":\"guest\",\"email\":\"guest#ibm.com\"}" http://localhost:8080/HelperSpringMVC/emp
* Adding handle: conn: 0x6a3400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x6a3400) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /HelperSpringMVC/emp HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
> Content-type:application/json
> Content-Length: 47
>
* upload completely sent off: 47 out of 47 bytes
< HTTP/1.1 200 OK
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 11 Sep 2013 21:07:47 GMT
<
[{"id":10,"name":"john","email":"email"},{"id":3,"name":"guest","email":"guest#ibm.com"}]
* Connection #0 to host localhost left intact
Note my controller is a bit different it is here:
#Controller
public class controller {
#RequestMapping(method=RequestMethod.POST, value="/emp")
public #ResponseBody List<Employee> addEmp(#RequestBody Employee e, BindingResult results) {
if (results.hasErrors()) {
return new ArrayList<Employee>();
}
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(10, "john", "email"));
list.add(e);
return list;
}
....
Shout if this doesn't help or you want any of my files.
I had a similar error when I posted to a normal http controller. My problem happened when the binding failed, the solution was that I needed to include BindingResult result. You could try adding that after your model for POST.
I am not sure if it's the syntax of curl, but my curl command is as follow and works for me (your -H option part is different from mine)
curl -v -X PUT -H "Content-Type:application/json" -d '{"id":123, "name":"User123"}' http://localhost:8080/restful/user
Here is how Employee class look like
package com.mkyong.common.bean;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="employee")
public class Employee {
private long id;
private String name;
private String email;
public Employee() {}
public Employee(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Here is my controller method after adding BindingResult
#RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")
public #ResponseBody EmployeeList updateEmp(#RequestBody Employee e, #PathVariable String id, BindingResult result) {
if(result.hasErrors()) {
System.out.println("Error ::: " +result.toString());
}
employeeDS.update(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}

How to intercept HttpEntity from json response [duplicate]

This question already has answers here:
Spring MVC 3.2 #ResponseBody interceptor
(4 answers)
Closed 8 years ago.
I'm using a Spring setup using the json mapping converters to send POJO classes as json back to the client.
E.g.:
#RequestMapping
#ResponseBody
public User getUser() {
User user = getUser();
return user;
}
This will return e.g. something like to the client:
{ 'username': 'My username', 'lastname': 'My lastname' }
I want to intercept all my controller actions to wrap the json in something like:
{
'status': 200,
'data': { 'username': 'My username', 'lastname': 'My lastname' }
}
What would be the best approach for this?
Simply, you can create a map like this
#RequestMapping(value = "/user")
#ResponseBody
public Map<String, Object> getUser(){
User user = new User();
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", 200);
map.put("data", user);
return map;
}
For common usage, you can create a POJO
public static class ResutInfo{
private int status;
private Object data;
//get set}
then write the controller method
#RequestMapping(value = "/user")
#ResponseBody
public ResutInfo getUser2(){
User user = new User();
ResutInfo resutInfo = new ResutInfo();
resutInfo.setStatus(200);
resutInfo.setData(user);
return resutInfo;
}
I find a possible solution for your question by extends MappingJackson2HttpMessageConverter
public class MyMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
#Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//outputMessage.getHeaders().add("code", DigestUtils.md5Hex(object.toString()));
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", 200);
map.put("data", object);
super.writeInternal(map, outputMessage); //To change body of overridden methods use File | Settings | File Templates.
}
}
Then add your own conventer bean to spring-mvc config file
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.xxx.utils.MyMappingJackson2HttpMessageConverter">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler/>
This method does work for each #ResponseBody handler method.
Note that it changes default Spring action, you should think about it.
You can also do something like this
<mvc:annotation-driven> <!-- this is added for validations to work -->
<mvc:message-converters register-defaults="true">
<ref bean="jsonMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="YourDtoWrapper"/>
</bean>
<bean id="YourDtoWrapper" class="com.YourDtoWrapper"/>
inside your YourDtoWrapper class you can do something like this
public void writeValue(JsonGenerator jgen, Object value) throws IOException, JsonGenerationException, JsonMappingException {
SomeDTO<Object> someDto = new SomeDTO<Object>("SUCCESS");//you can use an enum here
someDto.setDto(value);//here dto is a property of someDTO
super.writeValue(jgen, SomeDTO.getAsMap(someDto));
}
this will give u result some thing like this
{
serviceResult: "SUCCESS"
context: null
dto: {
id: 1
firstName: "test"
lastName: "admin"
email: "admin#test.com"
phone: 1234
userName: "admin"
password: null
createdBy: "admin"
createdDate: null
statusId: 1
statusName: "ACTIVE"
organisationId: 1
organisationName: "test"
userTypeId: 1
userTypeName: "ADMIN"
roleIds: [0]
}
}

Spring MVC 3 + JSON

I'm trying to use Spring MVC with JSON. It works great when a return an object from the controller, but when I try to make an AJAX call passing a custom object as parameter I'm getting HTTP 415 error.
My spring-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<tx:annotation-driven />
<context:annotation-config/>
<context:component-scan
base-package="com.sommer.controller" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.sommer.service" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- ========= [ADDED FOR JSON SUPPORT] ========= -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="es"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sommer"/>
<property name="username" value="root"/>
<property name="password" value="master"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="persistenceUnitName" value="sommerPersistenceUnit"></property>
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL"
p:showSql="true"
p:generateDdl="true"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
My controller:
#RequestMapping(value="/editJSON2",headers={"content-type=application/json,application/xml,application/x-www-form-urlencoded"})
public #ResponseBody ActionResult editJSON2(#RequestBody CustomerTO toEdit){
return new ActionResult(toEdit);
}
Classes:
public class ActionResult {
private Boolean success;
private String message;
private Object object;
public ActionResult(){
this.success = true;
this.object = null;
this.message = null;
}
public ActionResult(Boolean isSuccess,Object obj, String message){
this.success = isSuccess;
this.object = obj;
this.message = message;
}
public ActionResult(Object obj){
this.success = true;
this.object = obj;
this.message = "";
}
public ActionResult(String message){
this.success = false;
this.object = null;
this.message = message;
}
public void setError(String msg){
this.success = false;
this.message = msg;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
public class CustomerTO {
private Long id;
private String name;
private String email;
private TestObject[] items;
public TestObject[] getItems() {
return items;
}
public void setItems(TestObject[] items) {
this.items = items;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public DocumentType getDocumentType() {
return documentType;
}
public void setDocumentType(DocumentType documentType) {
this.documentType = documentType;
}
public String getDocumentNumber() {
return documentNumber;
}
public void setDocumentNumber(String documentNumber) {
this.documentNumber = documentNumber;
}
private String surname;
private String sex;
private DocumentType documentType;
private String documentNumber;
public CustomerTO() {
}
public CustomerTO(Customer customer) {
this.id = customer.getId();
this.documentNumber = customer.getDocumentNumber();
this.documentType = customer.getDocumentType();
this.name = customer.getName();
this.surname = customer.getSurname();
this.sex = Sex.MALE.equals(customer.getSex())?"M":"F";
this.email = customer.getEmail();
this.items = new TestObject[1];
TestObject tio = new TestObject();
tio.setText("ITEM !");
this.items[0] = tio;
}
My ajax call:
var currentCustomer = {
'id': $('#id').val()
,'name' :$('#name').val()
,'surname' :$('#surname').val()
,'documentType' :$('#documentType').val()
,'documentNumber' :$('#documentNumber').val()
,'sex' :$('#sex').val()
,'email' :$('#email').val()
};
// Send the request
$.post('editJSON2.html', {toEdit:currentCustomer}, function(response) {
alert('OK');
}, 'json');
The problem I think is here:
public #ResponseBody ActionResult editJSON2(#RequestBody CustomerTO toEdit)
I think #ResquestBody is not working for me. I also have
#RequestMapping("/editJSON")
public #ResponseBody ActionResult editJSON(#RequestParam(required=false) Long customerId){
CustomerTO toEdit = customerId!=null ? new CustomerTO(customerService.getById(customerId)):new CustomerTO();
return new ActionResult(toEdit);
}
And when I call it I have no problem.
This is information I collected from firebug:
Parámetrosapplication/x-www-form-urlencoded
toEdit[documentNumber] 36466
toEdit[documentType] DNI
toEdit[email] jpruizmdq#hotmail.com
toEdit[id] 2
toEdit[name] John
toEdit[surname] Doe
Código fuente
toEdit%5Bid%5D=2&toEdit%5Bname%5D=John&toEdit%5Bsurname%5D=Doe&toEdit%5BdocumentType%5D=DNI&toEdit%5BdocumentNumber%5D=36466&toEdit%5Bemail%5D=jpruizmdq%40hotmail.com
It's no tot working because content type of your request is application/x-www-form-urlencoded
and it supposed to be application/json
try to send it with Jquery the following way:
$.ajax({
type: "POST",
url: "someurl",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{id: '" + someId + "'}",
success: function(json) {
}};
Thanks! Problem solved. Here is the code
#RequestMapping(value="/editJSON2")
public #ResponseBody ActionResult editJSON2(#RequestBody CustomerTO toEdit){
return new ActionResult(toEdit);
}
ajaxCall('editJSON2.html',JSON.stringify(currentCustomer),function(valor){
alert('OK');
});
function ajaxCall(url,data,callback,onError){
jQuery.ajax({
url:url
,dataType: 'json'
,data:data
,type: "POST"
,contentType: "application/json; charset=utf-8"
,success:function(actionResult){
-----------
}
,error:function(jqXHR, textStatus, errorThrown){
---
}
});
}
It was simple! I added contentType: "application/json; charset=utf-8" and i used JSON.stringify(currentCustomer). With that it worked