我在尝试使用RESTFUL API
和Spring Framework
+ Spring Security
设置Jersey
时遇到了一些麻烦。
我可以部署一个Jersey
rest api并成功请求资源但是当我也实现Spring(Framework + Spring安全性)时,事情就会失控。
之前我曾使用旧的Spring
版本进行配置XML
文件,但我想玩,并了解如何仅使用Java
注释进行配置。我遇到了麻烦,因为我无法通过applicationContext.xml
。我只是不想放一个XML
文件,我只想用Java
注释配置我的小型api webapp。
当我将Spring Security
配置行添加到web.xml
并尝试请求任何资源时,出现此错误:
No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
因此,我还将此代码段添加到我的web.xml
:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
然后,我为Spring创建了一个新的配置类,一个简单的配置类:
@Configuration
@ComponentScan({
"com.myapp.rest.api",
"com.myapp.business.services"
})
public class SpringApplicationConfig {
}
我再次重新启动Tomcat
并试图请求任何资源,然后它开始抱怨找不到 applicationContext.xml
文件,我没有提供,但是我不知道为什么它仍然试图找到它,如果我有另一个带@Configuration
注释的类。如何在不使用Spring
的情况下配置applicationContext.xml
?
注意:我知道Spring MVC
和Spring Boot
,但我正在玩Jersey
并使用Spring Framework
进行依赖注入,Spring Security
显然也是为了授权和安全。
在web.xml
下方
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.myapp.rest.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- SPRING SECURITY -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我的pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>api</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>api</name>
<build>
<finalName>my api</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- JERSEY (2) -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<!-- Soporte JSON -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<!-- SPRING + SPRING SECURITY -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- JACKSON - JSON -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- LOG4J2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- JWT TOKEN -->
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${jose.jwt.version}</version>
</dependency>
</dependencies>
<properties>
<jose.jwt.version>4.39.2</jose.jwt.version>
<spring-security.version>4.2.3.RELEASE</spring-security.version>
<spring.version>4.3.9.RELEASE</spring.version>
<log4j.version>2.8.2</log4j.version>
<jersey.version>2.9.1</jersey.version>
<jackson.version>2.8.9</jackson.version>
<servlet-api.version>3.1.0</servlet-api.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
答案 0 :(得分:1)
您缺少web.xml中的一些重要内容
// For java config, you don't want to use .xml
// you have to include AnnotationConfigWebApplicationContext
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
// Location of your Configuration class
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.package.AppConfig</param-value>
</context-param>
// To load context
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
// Spring Servlet
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
// Your controllers
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>my.package.controllers.HomeController</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
// Servlet url mapping
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
添加控制器类
@Component
@Path("/home")
public class HomeController {
更新: 正如有人告诉问题的作者只对Spring DI感兴趣,不涉及Spring MVC,下面是简单的配置引导WebApplicationInitializer以编程方式配置ServletContext,而不使用web.xml。请注意,这将需要Servlet 3.0+容器。
public class ApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context
= new AnnotationConfigWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(context));
servletContext.setInitParameter(
"contextConfigLocation", "com.package.my");
}
}