Swagger 2 UI 无法访问,Spring 启动应用程序部署在外部 tomcat 上 [英] Swagger 2 UI not accessible ,Spring boot application deployed on external tomcat
问题描述
我已将 war 复制到 apache-tomcat-8.0.43/webapps 并使用 ./startup.sh
启动 tomcat.当我尝试访问 http://localhost:8080/swagger-ui.html 时,我得到 404,但是当我说 mvn spring-boot:run
时它正在工作.我缺少任何配置吗?下面的示例代码:
package com.user;导入 org.springframework.boot.SpringApplication;导入 org.springframework.boot.autoconfigure.SpringBootApplication;导入 org.springframework.context.annotation.Bean;导入 springfox.documentation.builders.RequestHandlerSelectors;导入 springfox.documentation.spi.DocumentationType;导入 springfox.documentation.spring.web.plugins.Docket;进口 springfox.documentation.swagger2.annotations.EnableSwagger2;导入静态 springfox.documentation.builders.PathSelectors.regex;@SpringBootApplication@EnableSwagger2公共类 UserMicroServicesApplication 扩展 SpringBootServletInitializer {私有静态 ConfigurableApplicationContext ctx;@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(UserMicroServicesApplication.class);}public static void main(String[] args) 抛出异常 {ctx = SpringApplication.run(UserMicroServicesApplication.class, args);}@豆角,扁豆公共 Docket productApi() {返回新的 Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.user.controller")).paths(regex("/api.*")).build().apiInfo(apiInfo());}私有 ApiInfo apiInfo() {return new ApiInfo("用户 API", "用户服务 API.", "V1", "服务条款", "", "", "");}}控制器类-------------------包 com.user.controller;导入 com.user.service.UserService;导入 com.user.vo.UserVO;导入 org.slf4j.Logger;导入 org.slf4j.LoggerFactory;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.http.HttpStatus;导入 org.springframework.http.MediaType;导入 org.springframework.web.bind.annotation.*;导入 javax.validation.Valid;导入 java.util.List;@RestController@RequestMapping(value = "/api")公共类用户控制器{私有静态最终记录器记录器 = LoggerFactory.getLogger(UserController.class);@自动连线私人用户服务用户服务;@RequestMapping(value = "/user", method = RequestMethod.POST, products = MediaType.APPLICATION_JSON_VALUE)@ResponseStatus(HttpStatus.CREATED)@ResponseBody公共 UserVO createUser(@RequestBody @Valid UserVO 用户) {logger.debug("使用电子邮件创建用户 = {}", user);UserVO updatedUserVO = userService.createUser(user);logger.debug("user created with id {}", updatedUserVO.getId());返回更新的用户VO;}}POM文件<包装>战争</包装><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/></父母><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-hateoas</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-rest</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><version>1.5.7.RELEASE</version></依赖>
您需要为链接添加上下文路径.
apache-tomcat-8.0.43/webapps/${war-name}
http://localhost:8080/${war-name}/swagger-ui.html>
I have copied the war to apache-tomcat-8.0.43/webapps and started tomcat using ./startup.sh
. When I try to access http://localhost:8080/swagger-ui.html, I get 404 but it's working when I say mvn spring-boot:run
. Am I missing any configuration? Sample code below:
package com.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
@SpringBootApplication
@EnableSwagger2
public class UserMicroServicesApplication extends SpringBootServletInitializer {
private static ConfigurableApplicationContext ctx;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(UserMicroServicesApplication.class);
}
public static void main(String[] args) throws Exception {
ctx = SpringApplication.run(UserMicroServicesApplication.class, args);
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.user.controller")).paths(regex("/api.*")).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo("User API", "User service API.", "V1", "Terms of service", "", "", "");
}
}
controller class
-------------------
package com.user.controller;
import com.user.service.UserService;
import com.user.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping(value = "/api")
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping(value = "/user", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public UserVO createUser(@RequestBody @Valid UserVO user) {
logger.debug("creating user with email = {}", user);
UserVO updatedUserVO = userService.createUser(user);
logger.debug("user created with id {}", updatedUserVO.getId());
return updatedUserVO;
}
}
POM.xml
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</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-hateoas</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-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
You need to add context path to the link.
apache-tomcat-8.0.43/webapps/${war-name}
http://localhost:8080/${war-name}/swagger-ui.html
这篇关于Swagger 2 UI 无法访问,Spring 启动应用程序部署在外部 tomcat 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!