また、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>
マイパルダオ(抜粋)
@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 を試しましたが、応答は次のように表示されます:「... 応答は構文的に正しくありません」
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 (?, ?, ?, ?, ?, ?)