Web services 如何使用JPA控制事务SpringRESTWebService?
我想知道如何在SpringRESTWebService中控制事务。 如果我在不使用WebserviceController的情况下使用JUnit-to-Service和DAO进行测试,就可以了。但是,当客户端程序调用webservice进程时,我得到以下错误 环境 配置Tomcat 7需要做什么 错误 Client.javaWeb services 如何使用JPA控制事务SpringRESTWebService?,web-services,spring-mvc,jpa,jta,Web Services,Spring Mvc,Jpa,Jta,我想知道如何在SpringRESTWebService中控制事务。 如果我在不使用WebserviceController的情况下使用JUnit-to-Service和DAO进行测试,就可以了。但是,当客户端程序调用webservice进程时,我得到以下错误 环境 配置Tomcat 7需要做什么 错误 Client.java RestTemplate restTemplate = new RestTemplate(); OperatorDTO operatorDTO = new Operator
RestTemplate restTemplate = new RestTemplate();
OperatorDTO operatorDTO = new OperatorDTO("CycDemo", "TTF", "01234684101");
String response = restTemplate.postForObject(SERVER_URI + URIConstant.OPERATOR_CREATE, operatorDTO, String.class);
服务器端
OperatorController.java
@Controller
public class OperatorController {
@Autowired
private IOperatorService operatorService;
@RequestMapping(value = URIConstant.OPERATOR_CREATE, method = RequestMethod.POST, produces = "application/json; charset=utf-8")
public @ResponseBody OperatorDTO createOperator(@RequestBody OperatorDTO operatorDTO) {
Operator operator = new Operator(operatorDTO);
operatorService.addNewOperator(operator);
return operatorDTO;
}
}
OperatorService.java
@Service(value = "OperatorService")
public class OperatorService implements IOperatorService {
@Resource(name = "OperatorDAO")
private IOperatorDAO OperatorDAO;
@Transactional(propagation = Propagation.REQUIRED)
public void addNewOperator(Operator Operator) throws SystemException {
try {
OperatorDAO.insert(Operator);
} catch (DAOException e) {
//throw exception
}
}
}
OperatorDAO.java
@Repository("OperatorDAO")
public class OperatorDAO implements IOperatorDAO {
// inject entity manager
@Transactional(propagation = Propagation.REQUIRED)
public void insert(Operator operator) throws DAOException {
try {
em.persist(operator);
em.flush();
} catch (PersistenceException pe) {
//throw exception
}
}
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-beans.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
servlet-context.xml
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<context:component-scan base-package="com.ignite.easyticket" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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">
<import resource="spring-beans.xml" />
<mvc:annotation-driven />
<context:component-scan base-package="com.ignite.easyticket" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
spring-beans.xml
<context:annotation-config/>
<context:component-scan base-package="com.ignite.easyticket"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<util:properties id="SQL_ERROR_CODE" location="classpath:SQL_ERROR_CODE.properties"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JPA"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/>
</property>
<property name="jpaPropertyMap">
<props>
<prop key="eclipselink.weaving">false</prop>
</props>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
</bean>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<property name="generateDdl" value="false"/>
<property name="showSql" value="true"/>
</bean>
我有一个解决办法。现在,一切都好了
我更改配置如下
servlet-context.xml
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<context:component-scan base-package="com.ignite.easyticket" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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">
<import resource="spring-beans.xml" />
<mvc:annotation-driven />
<context:component-scan base-package="com.ignite.easyticket" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>