Spring + MVC + Hibernate: Can't read my requestBody -


and again question converning sring, mvc , hibernate. tried every possible solution given here. nothing fixed problem.

what want do: add new entry sql database sending put-request server json object.

what works: server working, request supposed to. database recognizes , adds new entry, attributes null

my setup: database: sql (generated xampp) server: spring, mvc, hibernate

here files (please note inluded jacksonmapper)

applicationcontext-servlet.xml

<context:component-scan base-package="ma" />  <mvc:annotation-driven />  <tx:annotation-driven />  <!-- hibernate integration -->  <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"     destroy-method="close">     <property name="driverclassname" value="com.mysql.jdbc.driver" />     <property name="url" value="jdbc:mysql://localhost:3306/travelpal" />     <property name="username" value="root" />     <property name="password" value="" /> </bean>  <bean id="sessionfactory"     class="org.springframework.orm.hibernate4.localsessionfactorybean">     <property name="datasource" ref="datasource"></property>     <property name="hibernateproperties">         <props>             <prop key="hibernate.dialect">org.hibernate.dialect.mysql5dialect</prop>             <prop key="hibernate.show_sql">true</prop>         </props>     </property>     <property name="packagestoscan" value="ma" /> </bean>  <bean id="transactionmanager"     class="org.springframework.orm.hibernate4.hibernatetransactionmanager"     p:sessionfactory-ref="sessionfactory"> </bean>  <!-- added support json put --> <bean id="jsonconverter"     class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter">     <property name="prefixjson" value="false" />     <property name="supportedmediatypes" value="application/json" /> </bean> 

i tried add part appeared in lot of answers, spring doesn't allow me add it. displays error, stating "build path incomplete. cannot find class file javax/servlet/servletexception"

<bean id="annotationhandleradapter" class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter" >  <property name="order" value="1" />      <property name="messageconverters">      <list>         <bean class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter" >               <property name="supportedmediatypes" value="application/json"/>         </bean>         <bean class = "org.springframework.http.converter.stringhttpmessageconverter">             <property name="supportedmediatypes" value = "text/plain;charset=utf-8" />         </bean>      </list> </property> 

my pom.xml

<dependencies>     <!-- spring 3 dependencies -->     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-core</artifactid>         <version>3.1.1.release</version>     </dependency>      <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-web</artifactid>         <version>3.1.1.release</version>     </dependency>      <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-webmvc</artifactid>         <version>3.1.1.release</version>     </dependency>      <!-- jackson json mapper -->     <dependency>         <groupid>org.codehaus.jackson</groupid>         <artifactid>jackson-mapper-asl</artifactid>         <version>1.9.12</version>     </dependency>      <!-- hibernate integration start -->      <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-orm</artifactid>         <version>3.2.0.release</version>     </dependency>      <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-core</artifactid>         <version>4.1.9.final</version>     </dependency>      <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-tx</artifactid>         <version>3.1.2.release</version>     </dependency>      <dependency>         <groupid>commons-dbcp</groupid>         <artifactid>commons-dbcp</artifactid>         <version>1.2.2</version>     </dependency>      <!-- testint added -->     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-dao</artifactid>         <version>2.0.8</version>     </dependency>  </dependencies>    <build>     <sourcedirectory>src</sourcedirectory>     <plugins>         <plugin>             <artifactid>maven-compiler-plugin</artifactid>             <version>3.0</version>             <configuration>                 <source>1.6</source>                 <target>1.6</target>             </configuration>         </plugin>         <plugin>             <artifactid>maven-war-plugin</artifactid>             <version>2.3</version>             <configuration>                 <warsourcedirectory>webcontent</warsourcedirectory>                 <failonmissingwebxml>false</failonmissingwebxml>             </configuration>         </plugin>     </plugins> </build> 

my web.xml

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>travelpalserver</display-name> <welcome-file-list>     <welcome-file>index.html</welcome-file>     <welcome-file>index.htm</welcome-file>     <welcome-file>index.jsp</welcome-file>     <welcome-file>default.html</welcome-file>     <welcome-file>default.htm</welcome-file>     <welcome-file>default.jsp</welcome-file> </welcome-file-list>   <context-param>     <param-name>contextconfiglocation</param-name>     <param-value>/web-inf/applicationcontext-servlet.xml</param-value> </context-param> <listener>     <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener>  <servlet>     <servlet-name>applicationcontext</servlet-name>     <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>     <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>     <servlet-name>applicationcontext</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping>  <!--  added json put --> <filter>     <filter-name>httpmethodfilter</filter-name>     <filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class> </filter>  <filter-mapping>     <filter-name>httpmethodfilter</filter-name>     <servlet-name>applicationcontext</servlet-name> </filter-mapping> 

my paldao (excerpt)

    @override public void insertpal(pal pal) {     system.out.println("inserting pal: " + pal.getname());     sessionfactory.getcurrentsession().save(pal); } 

pal.getname() returns null

the palcontroller

    @requestmapping(value="/", method = requestmethod.put) public @responsebody pal insertpal(@modelattribute pal person) {     system.out.println("im controller; adding pal: " + person.getname());     palservice.insertpal(person);     return person; } 

here tried @requestbody, response says: "... response syntactically incorrect" also, person.getname() returns null

pal.java

@entity @table(name="pal") public class pal {  @id   @generatedvalue(strategy = generationtype.auto)   @column(name="id", nullable = false) private long id;  @column(name="name", nullable = true) private string name;  @column(name="age", nullable = false) private int age;  @column(name="gender", nullable = false) private string gender;  @manytoone @joincolumn(name="locationid") private location locationid;  @manytoone @joincolumn(name="settingid") private settings settingid;   public pal() {}  public pal(long id, string name, string gender, location locationid, settings setting) {     super();     this.id = id;     this.name = name;     this.gender = gender;     this.locationid = locationid;     this.settingid = setting; }  public pal(long id, string name, int alter) {     super();     this.id = id;     this.name = name;     this.age = alter; }  public pal(string name, int alter) {     super();     this.name = name;     this.age = alter; }  public pal(string name, int age, string gender) {     super();     this.name = name;     this.age = age;     this.gender = gender; }  public pal(long id, string name, int age, location locationid) {     super();     this.id = id;     this.name = name;     this.age = age;     this.locationid = locationid; }  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 int getage() {     return age; }  public void setage(int age) {     this.age = age; }  public string getgender() {     return gender; }  public void setgender(string gender) {     this.gender = gender; }  public location getposition() {     return locationid; }  public void setposition(location locationid) {     this.locationid = locationid; }  public settings getsetting() {     return settingid; }  public void setsetting(settings setting) {     this.settingid = setting; }    public location getlocationid() {     return locationid; }  public void setlocationid(location locationid) {     this.locationid = locationid; } 

i hope can me this. i'm more frustrated now.

edit

i send request restclient (firefox addon) url : localhost:8080/travelpalserver/pal/ , requestbody is:

{ "name": "elena", "age": 22, "gender": "female" } 

i tried not putting "" around attribute identifiers; doesn't change thing when send request ios app (within emulator) same server response (so restclient not problem)

that appears in console, after send put-request

hibernate: insert pal (age, gender, locationid, name, nationalityid, settingid) values (?, ?, ?, ?, ?, ?) 

i think need following:

a) specify name @modelattribute

public @responsebody pal insertpal(@modelattribute("person") pal person) { 

b) need submit form using mime type application/x-www-form-urlencoded. example:

person=<json-string-here> 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -