[英]Configuring Spring RESTFUL API webapp with Java
I'm having some trouble trying to set up a RESTFUL API
using Spring Framework
and Spring Security
+ Jersey
. 我在尝试使用
Spring Framework
和Spring Security
+ Jersey
设置RESTFUL API
遇到了一些麻烦。
I could deploy a Jersey
rest api and request resources successfully but when I also implemented Spring (Framework + Spring security) things are getting out of hand. 我可以部署
Jersey
休息api并成功请求资源,但是当我还实现Spring(框架+ Spring安全性)时,事情就变得一发不可收拾。
I've worked before with old Spring
versions where configuration was made with XML
files, but I would like to play and learn how to config it with Java
annotations only. 我以前使用过旧的
Spring
版本,其中使用XML
文件进行配置,但是我想玩和学习如何仅使用Java
注释进行配置。 I'm having trouble with this as I can't get past applicationContext.xml
. 我遇到了麻烦,因为我无法通过
applicationContext.xml
。 I simply don't want to put an XML
file, I just want to configure my small rest api webapp with Java
annotations only. 我只是不想放置
XML
文件,而只想用Java
注释配置我的小型REST API XML
应用Java
。
When I added Spring Security
config lines to my web.xml
and tried to request any resource, this error appeared: 当我将
Spring Security
配置行添加到我的web.xml
并尝试请求任何资源时,出现此错误:
No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
So, I also added this snippet to my web.xml
: 因此,我也将此代码段添加到了我的
web.xml
:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Then, I created a new configuration class for Spring, a simple one: 然后,我为Spring创建了一个新的配置类,一个简单的类:
@Configuration
@ComponentScan({
"com.myapp.rest.api",
"com.myapp.business.services"
})
public class SpringApplicationConfig {
}
I restarted my Tomcat
again and tried to request any resource, then it started to complain about not finding the applicationContext.xml
file, which I didn't provide, but I don't know why it is still trying to find it if I have another class with @Configuration
annotation. 我再次重新启动了
Tomcat
并尝试请求任何资源,然后它开始抱怨找不到我没有提供的applicationContext.xml
文件,但是我不知道为什么如果有的话它仍在尝试查找它另一个带有@Configuration
批注的类。 How can I configure Spring
without using applicationContext.xml
? 如何不使用
applicationContext.xml
来配置Spring
?
Note: I am aware of Spring MVC
and Spring Boot
, but I'm currently playing with Jersey
and using Spring Framework
for dependency injection, also Spring Security
for auth and security stuff obviously. 注意:我知道
Spring MVC
和Spring Boot
,但是我目前正在和Jersey
一起玩,并且使用Spring Framework
进行依赖项注入,显然还使用Spring Security
进行身份验证和安全性工作。
Below my web.xml
: 在我的
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>
And my pom.xml
: 还有我的
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>
You are missing some important points in web.xml 您缺少了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>
Adding controller class 添加控制器类
@Component
@Path("/home")
public class HomeController {
UPDATE : As it was told that author of question is interested in just Spring DI, not involving Spring MVC, below is simple config bootstrapping WebApplicationInitializer to configure the ServletContext programmatically, without web.xml. 更新:据说问题作者只对Spring DI感兴趣,不涉及Spring MVC,下面是简单的配置引导WebApplicationInitializer,以编程方式配置ServletContext,而没有web.xml。 Note that this would require Servlet 3.0+ container.
注意,这将需要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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.