再次回归Sring,MVC和Hibernate的问题。我在这里尝试了所有可能的解决方案但没有解决我的问题。
我想做什么: 通过使用JSON对象向我的服务器发送PUT-Request,将新条目添加到我的SQl数据库中。
什么有效: 我的服务器正在运行,所有GET请求都按照预期执行。 此外,我的数据库识别并添加一个新条目,但所有属性都是NULL
我的设置: 数据库:SQL(使用XAMPP生成) 服务器:Spring,MVC,Hibernate
以下是我的文件(请注意我已经包含了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 to 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>
我尝试添加此部分,因为它出现在很多答案中,但Spring不允许我添加它。它始终显示错误,指出“构建路径不完整。无法找到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>
我的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>
我的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 for 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>
我的PalDAO (摘录)
@Override
public void insertPal(Pal pal) {
System.out.println("Inserting Pal: " + pal.getName());
sessionFactory.getCurrentSession().save(pal);
}
pal.getName()返回NULL
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;
}
在这里我也尝试了@RequestBody,然后回复说:“......响应在语法上是不正确的” 此外,person.getName()返回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;
}
我真的希望有人可以帮助我。我现在非常沮丧。
修改
我用RestClient发送请求(Firefox Addon) URL:localhost:8080 / TravelPalServer / pal / RequestBody是:
{
"name": "Elena",
"age": 22,
"gender": "Female"
}
我也试过不在属性标识符周围加上“”;不会改变一件事 当我从我的iOS应用程序(在模拟器中)发送请求时,我得到相同的服务器响应(因此RestClient不是问题)
发送PUT-Request
后,控制台中显示的内容Hibernate: insert into Pal (age, gender, locationId, name, nationalityId, settingId) values (?, ?, ?, ?, ?, ?)
答案 0 :(得分:0)
我认为您需要执行以下操作:
a)指定@ModelAttribute的名称
public @ResponseBody Pal insertPal(@ModelAttribute("person") Pal person) {
b)您需要使用MIME类型application/x-www-form-urlencoded
提交表单。例如:
person=<json-string-here>