1

我正在使用 Spring 3.0 rest、hibernate 3 和 Android 4。尝试在我的数据库表中添加用户时,事情停止工作。在帖子上设置的对象,使用 Android 和 Spring 3.1 始终为空。这是在服务器端,在 Spring 控制器中:

@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>

Servlet.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具有值。我在客户端没有收到任何错误,它直接到服务器之一,但它到达为空。我根本不明白这一点,尝试使用@RequestBody,它甚至没有进入我的控制器方法。你以前有没有遇到过这个,任何想法都会很棒。以前有人遇到过这个错误吗?我现在真的不知道该尝试什么了。谢谢!

4

0 回答 0