【问题标题】:Swagger 2 UI not accessible ,Spring boot application deployed on external tomcatSwagger 2 UI 无法访问,Spring Boot 应用程序部署在外部 tomcat 上
【发布时间】:2018-05-29 00:12:22
【问题描述】:

我已将战争复制到 apache-tomcat-8.0.43/webapps 并使用 ./startup.sh 启动 tomcat。当我尝试访问 http://localhost:8080/swagger-ui.html 时,我得到 404,但当我说 mvn spring-boot:run 时它正在工作。我是否缺少任何配置?示例代码如下:

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>

【问题讨论】:

    标签: rest maven tomcat swagger-ui


    【解决方案1】:

    您需要为链接添加上下文路径。

    apache-tomcat-8.0.43/webapps/${war-name}

    http://localhost:8080/${war-name}/swagger-ui.html

    【讨论】:

    • 我正在添加这个它可能对一些想要使用没有战争名称的 localhost 有帮助,我添加了 J2EE 框架支持(之前没有)并在 intellij 中配置了 tomcat 服务器,这改变了你可以在没有战争名称的情况下访问 api,即localhost:8080/swagger-ui.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2021-11-01
    • 2021-11-06
    • 2021-05-24
    • 1970-01-01
    • 2020-11-27
    • 2020-03-16
    相关资源
    最近更新 更多