Java @Spring MVC中ServletDispatcher未映射瞬态@RestController
我目前面临着Spring MVC的一个问题:如果我创建一个rest控制器并将其标记为Java @Spring MVC中ServletDispatcher未映射瞬态@RestController,java,spring,spring-mvc,spring-transactions,Java,Spring,Spring Mvc,Spring Transactions,我目前面临着Spring MVC的一个问题:如果我创建一个rest控制器并将其标记为@Transient,它不会被ServletDispatcher(404,如果被调用)映射,如果我删除注释,控制器将正确映射,一切正常。如果我将一个服务标记为事务性的,并从控制器内部调用它,那么一切都正常(事务正确启动和完成)。不幸的是,现在将我的服务标记为事务性服务是不可行的(我正在使用遗留系统)。在撰写本文时,我正在使用最新的spring版本(4.3.9) 我当前的配置: <servlet>
@Transient
,它不会被ServletDispatcher
(404,如果被调用)映射,如果我删除注释,控制器将正确映射,一切正常。如果我将一个服务标记为事务性的,并从控制器内部调用它,那么一切都正常(事务正确启动和完成)。不幸的是,现在将我的服务标记为事务性服务是不可行的(我正在使用遗留系统)。在撰写本文时,我正在使用最新的spring版本(4.3.9)
我当前的配置:
<servlet>
<servlet-name>api</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/api-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
我的控制器:
@RestController
@RequestMapping("/accounts")
public class AccountsController {
@GetMapping
@Transactional
public List<Account> doStuff() {
//stuff...
return stuff;
}
}
@RestController
@请求映射(“/accounts”)
公共类帐户控制器{
@GetMapping
@交易的
公共列表doStuff(){
//东西。。。
归还物品;
}
}
My api-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
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.2.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.1.xsd">
<context:component-scan base-package="my.package"/>
</beans>
我省略了我的
applicationContext.xml
,因为它没有任何重要内容(只有一堆BL bean和一个包扫描)。多亏了m.Deinum的评论,我找到了一个解决方案。在我的例子中,我必须将代理目标类设置为true
@EnableTransactionManagement(proxyTargetClass=true)
您可以在此处找到更多详细信息:让您的web层具有事务性是一个坏主意。您的事务不应依赖于控制器是否成功显示结果这一事实。除此之外,在web层上使用代理(
@Transactional
创建代理)需要特定的设置,如中所述。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
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.2.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.1.xsd">
<context:component-scan base-package="my.package"/>
</beans>