Java 在Struts 2中使用Spring控制器创建REST API

Java 在Struts 2中使用Spring控制器创建REST API,java,spring,rest,spring-mvc,struts2,Java,Spring,Rest,Spring Mvc,Struts2,我有一个应用程序,它使用Struts(版本2.3.16.1 GA)和Spring(版本3.2.4 GA)将对象注入到动作中,等等 现在,我需要为我的应用程序创建一个RESTAPI,我不能使用Struts的REST插件,所以我认为既然Spring已经存在,我可以创建Spring控制器,然后用Spring为API提供服务 假设我当前的应用程序在服务器上的app上下文中运行,那么我将按如下方式访问它: http://www.myserver.com:8080/app/some/path/anActio

我有一个应用程序,它使用Struts(版本2.3.16.1 GA)和Spring(版本3.2.4 GA)将对象注入到动作中,等等

现在,我需要为我的应用程序创建一个RESTAPI,我不能使用Struts的REST插件,所以我认为既然Spring已经存在,我可以创建Spring控制器,然后用Spring为API提供服务

假设我当前的应用程序在服务器上的
app
上下文中运行,那么我将按如下方式访问它:

http://www.myserver.com:8080/app/some/path/anAction.action
这就是我当前应用程序的整个流程的工作原理。现在我打算做的是创建一个特定的路径,该路径将由我的REST API专用,因此我尝试让它像这样工作,例如:

http://www.myserver.com:8080/app/api/users
我想把
/api/*
映射到Spring,这样我所有的控制器都将住在那里

这里是我的配置的相关部分,我是为了完成这一点而做的

web.xml

INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-2) FrameworkServlet 'rest-api': initialization completed in 8939 ms
INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /app
INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "app.ear"
INFO  [com.app.web.filter.SignOnFilter] (http--0.0.0.0-8443-5) Redirecting to signon: /signon.action?redirectUri=index.action
WARN  [org.springframework.web.servlet.PageNotFound] (http--0.0.0.0-8443-3) No mapping found for HTTP request with URI [/app/api/users] in DispatcherServlet with name 'rest-api'

支柱准备
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter
struts执行
org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
支柱准备
/*
要求
向前地
struts执行
/*
要求
向前地
RESTAPI
org.springframework.web.servlet.DispatcherServlet
1.
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
com.myapp.spring.SpringApplicationConfiguration
RESTAPI
/原料药/*
struts.xml

INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-2) FrameworkServlet 'rest-api': initialization completed in 8939 ms
INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /app
INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "app.ear"
INFO  [com.app.web.filter.SignOnFilter] (http--0.0.0.0-8443-5) Redirecting to signon: /signon.action?redirectUri=index.action
WARN  [org.springframework.web.servlet.PageNotFound] (http--0.0.0.0-8443-3) No mapping found for HTTP request with URI [/app/api/users] in DispatcherServlet with name 'rest-api'

SpringApplicationConfiguration.java

INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-2) FrameworkServlet 'rest-api': initialization completed in 8939 ms
INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /app
INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "app.ear"
INFO  [com.app.web.filter.SignOnFilter] (http--0.0.0.0-8443-5) Redirecting to signon: /signon.action?redirectUri=index.action
WARN  [org.springframework.web.servlet.PageNotFound] (http--0.0.0.0-8443-3) No mapping found for HTTP request with URI [/app/api/users] in DispatcherServlet with name 'rest-api'
@配置
@EnableWebMvc
@ComponentScan(basePackages={“com.myapp.controller”})
@导入({BaseConfig.class,DataBaseConfiguration.class})
公共类SpringApplicationConfiguration{}
UserController.java

INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-2) FrameworkServlet 'rest-api': initialization completed in 8939 ms
INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /app
INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "app.ear"
INFO  [com.app.web.filter.SignOnFilter] (http--0.0.0.0-8443-5) Redirecting to signon: /signon.action?redirectUri=index.action
WARN  [org.springframework.web.servlet.PageNotFound] (http--0.0.0.0-8443-3) No mapping found for HTTP request with URI [/app/api/users] in DispatcherServlet with name 'rest-api'
@控制器
公共类用户控制器{
@自动连线
私人用户服务;
@RequestMapping(value=“/users”,method=GET)
@ResponseBy公共列表(@RequestParam(defaultValue=“25”)int-limit、@RequestParam(defaultValue=“1”)int-offset、@RequestParam字符串筛选器){
退货服务列表(限额、抵销、筛选);
}
}
这就是我认为我应该拥有的所有配置。现在,我启动服务器,登录应用程序(检查Struts是否正常工作),然后尝试向我的REST端点发送GET请求,下面是日志:

日志:

INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-2) FrameworkServlet 'rest-api': initialization completed in 8939 ms
INFO  [org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /app
INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "app.ear"
INFO  [com.app.web.filter.SignOnFilter] (http--0.0.0.0-8443-5) Redirecting to signon: /signon.action?redirectUri=index.action
WARN  [org.springframework.web.servlet.PageNotFound] (http--0.0.0.0-8443-3) No mapping found for HTTP request with URI [/app/api/users] in DispatcherServlet with name 'rest-api'

没有映射是因为没有正确映射

@RequestMapping(value="/api/users", method=GET)

问题在于在struts.xml中定义模式。 改变




谢谢。我已经试过了,但我得到的警告与我在原始问题上发布的警告完全相同。+1,我喜欢这个问题:)但是你展示了全部吗?什么是SignOnFilter,它在日志中,但不在代码中?为什么不能使用S2 rest插件?@AleksandrM我不能使用它,因为据我所知,它依赖于约定插件,这将需要对当前代码进行大量重构,才能使其正常工作way@AndreaLigiosSignOnFilter只是在我登录应用程序时执行一些授权逻辑。我确实在这里注册了,但我这么做只是为了测试常规流程是否仍然有效。对RESTAPI servlet的调用不应通过该过滤器。这可能只是给问题增加了额外的噪音,但在REST请求的常规流程中并不需要这样做