0

又是一个关于 Sring、MVC 和 Hibernate 的问题。我尝试了这里给出的所有可能的解决方案。但没有什么能解决我的问题。

我想做的事: 通过向我的服务器发送一个带有 JSON 对象的 PUT 请求,在我的 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

伙伴控制器

    @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

帕尔.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 (?, ?, ?, ?, ?, ?)
4

1 回答 1

0

我认为您需要执行以下操作:

a) 为您的 @ModelAttribute 指定一个名称

public @ResponseBody Pal insertPal(@ModelAttribute("person") Pal person) {

b) 您需要使用 MIME type 提交表单application/x-www-form-urlencoded。例如:

person=<json-string-here>
于 2013-07-12T03:26:56.880 回答