Spring 3.0 レスト、休止状態 3、および Android 4 を使用しています。db テーブルにユーザーを追加しようとすると、動作が停止します。Android と Spring 3.1 を使用してポストに設定されたオブジェクトは常に null です。これは、Spring Controller のサーバー側にあります。
@Transactional(readOnly = false)
@RequestMapping(value = "/user/add/", method = RequestMethod.POST,
headers = "Accept=application/json,text/html,application/xhtml+xml,application/xml",
consumes ="{MediaType.APPLICATION_JSON_VALUE}", produces = "{MediaType.APPLICATION_JSON_VALUE}")
public @ResponseBody
User registerUser(@ModelAttribute("user") User user, HttpServletResponse response) throws IOException {
return userService.saveUser(user);
}
ここで、ユーザーは常に null であり、もちろん db に挿入されます..null 値。
Androidからこれは私のオブジェクトを送信する方法です:
try {
RestTemplate restTemplate = new RestTemplate();
GenericEntityWithJson getEntityHeader = new GenericEntityWithJson();
HttpEntity<Taxi> entityHeader = getEntityHeader
.getHttpEntityWithJsonHeaders(user);
response = restTemplate.postForObject(url, entityHeader, User.class);
} catch (Exception e) {
System.out.println("register error: " + e.getMessage());
}
私のpersistence.xmlコード:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Load in application properties reference -->
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:be/taximobile/rest/resources/database.properties"/>
</bean>
<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref local="mysqlDataSource" />
</property>
<property name="annotatedClasses">
<list>
<value>be.taximobile.rest.bean.Client</value>
<value>be.taximobile.rest.bean.Taxi</value>
<value>be.taximobile.rest.bean.ClientCommand</value>
<value>be.taximobile.rest.bean.TaxiFeatures</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="lazy">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<!-- Configure Transaction manager for a single Hibernate SessionFactory(because we are using one database source only, and so a single Session Factory) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="rollbackOnCommitFailure" value="true" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- define DAO and Service beans -->
<bean id="clientDao" class="be.taximobile.rest.persistence.ClientDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="featureDao" class="be.taximobile.rest.persistence.FeatureDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="orderDao" class="be.taximobile.rest.persistence.OrderDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="taxiDao" class="be.taximobile.rest.persistence.TaxiDAOImpl">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="orderService"
class="be.taximobile.rest.services.OrderServiceImpl" >
<property name="orderDao">
<ref bean="orderDao" />
</property>
</bean>
<bean id="taxiService"
class="be.taximobile.rest.services.TaxiServiceImpl" >
<property name="taxiDao">
<ref bean="taxiDao" />
</property>
<property name="featureDao">
<ref bean="featureDao" />
</property>
</bean>
<bean id="featureService"
class="be.taximobile.rest.services.FeatureServiceImpl" >
<property name="featureDao">
<ref bean="featureDao" />
</property>
</bean>
<bean id="clientService"
class="be.taximobile.rest.services.ClientServiceImpl" >
<property name="clientDao">
<ref bean="clientDao" />
</property>
</bean>
<!-- end
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushModeName" value="FLUSH_AUTO" />
</bean> -->
</beans>
サーブレット.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- To enable autodetection of such annotated controllers, you add component scanning to your configuration. -->
<context:component-scan base-package="be.taximobile.rest.controller" />
<!-- hibernate configuration
<import resource="TaxiMobileBackend-persistence.xml" />-->
<!-- To enable @RequestMapping process on type level and method level -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<ref bean="marshallingConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<!-- JAXB2 marshaller. Automagically turns beans into xml -->
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>be.taximobile.rest.bean.Client</value>
<value>be.taximobile.rest.bean.Taxi</value>
<value>be.taximobile.rest.bean.ClientCommand</value>
<value>be.taximobile.rest.bean.TaxiFeatures</value>
</list>
</property>
</bean>
<!-- Mape views -->
<bean id="jsonview" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean id="taxis" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean id="orders" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean id="taxi" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean id="client" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<bean id="order" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>
<!-- end -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="text/plain"/>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<!--
<entry key="xml" value="application/xml"/>
<entry key="html" value="text/html"/>
-->
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<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/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<!-- Controllers -->
<bean id="taxiController" class="be.taximobile.rest.controller.TaxiController">
<property name="taxiService" ref="taxiService" />
</bean>
<bean id="clientController" class="be.taximobile.rest.controller.ClientController">
<property name="clientService" ref="clientService" />
</bean>
<bean id="orderController" class="be.taximobile.rest.controller.OrderController">
<property name="orderService" ref="orderService" />
<property name="taxiService" ref="taxiService" />
</bean>
<bean id="featuresController" class="be.taximobile.rest.controller.TaxiFeaturesController">
<property name="featureService" ref="featureService" />
</bean>
</beans>
WEB.xml ファイル:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="true">
<display-name>be.taximobile.rest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- The context params that read by ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/TaxiMobileBackend-context.xml
</param-value>
</context-param>
<!-- This listener will load other application context file in addition to springweb-servlet.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>TaxiMobileBackend</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TaxiMobileBackend</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tags/c.tld</taglib-location>
</taglib>
</jsp-config>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
投稿アクションの前に、私のオブジェクトuser
には値があります。クライアント側でエラーを受信しません。サーバー側に直接送信されますが、null に到達します。@RequestBody を使用してみましたが、コントローラーメソッドに入ることさえありません。以前にこれに遭遇したことがありますか?どんなアイデアでも素晴らしいでしょう。以前にこのエラーが発生した人はいますか? 現時点では、もう何を試すべきか本当にわかりません。ありがとうございました!