我想将前缀应用于rest api路径/api
:
我在application.properties中添加了以下几行:
spring.data.rest.basePath=/api
server.servlet.context-path=/api
spring.jackson.default-property-inclusion=non_null
但是在将代码部署到Wildfly中后,它无法正常工作:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<finalName>datalis_admin</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>10</source>
<target>10</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
应用程序启动方法:
@SpringBootApplication(scanBasePackages = { "org.admin.*", "org.plugin.service" })
@EntityScan("org.plugin.entity")
public class Application extends SpringBootServletInitializer implements WebMvcConfigurer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
由于某些原因,/api
未添加到映射的端点:
2018-10-08 12:32:19,060 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/transactions/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.PaymentTransactionsController.get(java.lang.String)
2018-10-08 12:32:19,061 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/transactions/{id}],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.PaymentTransactionsController.save(java.lang.String,org.datalis.admin.backend.restapi.dto.PaymentTransactionsDTO)
2018-10-08 12:32:19,064 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/transactions],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.PaymentTransactionsController.create(org.datalis.admin.backend.restapi.dto.PaymentTransactionsDTO)
2018-10-08 12:32:19,065 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/transactions],methods=[GET]}" onto public org.springframework.data.domain.Page<org.datalis.admin.backend.restapi.dto.PaymentTransactionsDTO> org.datalis.admin.backend.restapi.PaymentTransactionsController.find(org.datalis.admin.backend.restapi.dto.PaymentTransactionsFilterDTO,org.springframework.data.domain.Pageable)
2018-10-08 12:32:19,069 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/transactions/export],methods=[GET]}" onto public void org.datalis.admin.backend.restapi.PaymentTransactionsController.export(javax.servlet.http.HttpServletResponse)
2018-10-08 12:32:19,073 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/transactions/types],methods=[GET]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.PaymentTransactionsController.getTypes()
2018-10-08 12:32:19,078 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/users/reset],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.UserController.reset(org.datalis.admin.backend.restapi.dto.PasswordResetDTO)
2018-10-08 12:32:19,079 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/users/token],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.UserController.token(org.datalis.admin.backend.restapi.dto.PasswordResetDTO)
2018-10-08 12:32:19,080 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/users/request],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.datalis.admin.backend.restapi.UserController.resetRequest(org.datalis.admin.backend.restapi.dto.PasswordResetDTO)
2018-10-08 12:32:19,092 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-08 12:32:19,093 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 81) Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
答案 0 :(得分:2)
Spring Boot支持的所有server.*
属性仅适用于嵌入式servlet容器(Tomcat, Jetty, or Undertow)
的配置。
如果要将Spring Boot应用程序部署到独立服务器/ Jboss Wildfly,则需要使用其提供的任何机制来配置该服务器。
请通过jboss-web.xml文件中的设置配置上下文根变量。
这是您可以在部署的应用程序中添加/WEB-INF/jboss-web.xml文件的方法:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<context-root>/api</context-root>
</jboss-web>
更新
如果应用程序的名称为package_admin,并且未配置上下文根,则应使用整个文件名来调用该应用程序;因此,在您的情况下,如果您想将api作为wildfly中的默认上下文路径,那么如果您尚未明确配置上下文路径,则使用api作为您的war文件名。
但是,我们希望仅使用更简单的上下文根(例如/ api /)来调用应用程序,但文件名始终需要管理版本控制等,因此配置应用程序的上下文根始终是个好主意。
要执行此配置,必须创建一个新文件 在应用程序的WEB-INF文件夹中;这是同一目录 web.xml文件所在的位置。
此设置是针对名为jboss-web.xml的文件进行的,该文件应为 放在WEB-INF ...
答案 1 :(得分:1)
我已经使用AbstractController
@RequestMapping("api")
public abstract class AbstractController {}
其他控制器将扩展这一范围
我尚未测试,但也可以使用以下属性server.servlet.context-path
但是我认为这仅在您使用mvn spring-boot:run
运行项目时有效,因为servlet引擎或应用程序服务器具有用于分配已部署的战争应用程序的路径的描述符。