[英]404 when use swagger2-ui with spring-boot
I'm trying to make documentation on the code I'm working with using Spring-boot , swagger2 and H2-database.我正在尝试使用 Spring-boot 、 swagger2 和 H2-database 制作有关我正在使用的代码的文档。
Here is the swaggerconfig这是 swaggerconfig
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors
.basePackage("se.dala.restserviceswagger.controller"))
.paths(PathSelectors.any())
.build();
}
}
And here is the Controller这是控制器
@RestController
public class EmployeeController {
private final EmployeeRepository repository;
private final EmployeeResourceAssembler assembler;
public EmployeeController(EmployeeRepository repository, EmployeeResourceAssembler assembler) {
this.repository = repository;
this.assembler = assembler;
}
@GetMapping("/employees/{id}")
public Resource<Employee> get(@PathVariable Long id) {
Employee employee = repository.findById(id).orElseThrow(() -> new EmployeeNotFoundException(id));
return assembler.toResource(employee);
}
@GetMapping("/employees")
public Resources<Resource<Employee>> getAll() {
List<Resource<Employee>> employees = repository.findAll().stream()
.map(assembler::toResource)
.collect(Collectors.toList());
return new Resources<>(employees,
linkTo(methodOn(EmployeeController.class).getAll()).withSelfRel());
}
//Blanda inte ihop resource.getId() med employee.getId(). resource.getId() ger dig en URI.
@PostMapping("/employees")
public ResponseEntity<?> newEmployee(@RequestBody Employee newEmployee) throws URISyntaxException {
Resource<Employee> resource = assembler.toResource(repository.save(newEmployee));
return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource);
}
@PutMapping("/employees/{id}")
public ResponseEntity<?> replace(@RequestBody Employee newEmployee, @PathVariable Long id) throws URISyntaxException {
Employee updatedEmployee = repository.findById(id).map(employee -> {
employee.setName(newEmployee.getName());
employee.setRole(newEmployee.getRole());
return repository.save(employee);
}).orElseGet(() -> {
newEmployee.setId(id);
return repository.save(newEmployee);
});
Resource<Employee> resource = assembler.toResource(updatedEmployee);
return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource);
}
@DeleteMapping("/employees/{id}")
public ResponseEntity<?> delete(@PathVariable Long id) {
repository.deleteById(id);
return ResponseEntity.noContent().build();
}
}
And here is my pom.这是我的 pom。
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I go to http://localhost:8080/v2/api-docs I get it to work but not when I go to http://localhost:8080/swagger-ui.html .当我访问http://localhost:8080/v2/api-docs 时,它可以正常工作,但当我访问http://localhost:8080/swagger-ui.html 时却没有。 There I get: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.我得到:Whitelabel Error Page 该应用程序没有明确的 /error 映射,因此您将其视为后备。 Thu Feb 14 15:22:38 CET 2019 There was an unexpected error (type=Not Found, status=404). 2019 年 2 月 14 日星期四 15:22:38 CET 出现意外错误(类型=未找到,状态=404)。 No message available没有可用的消息
I don't have any Spring security or anything.我没有任何 Spring 安全或任何东西。
Use SwaggerConfig
without extending WebMvcConfigurationSupport
should work then.使用SwaggerConfig
而不扩展WebMvcConfigurationSupport
应该可以工作。
Error is not related to security its telling that there is no mapped path with that url.错误与安全性无关,它表明该 url 没有映射路径。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors
.basePackage("se.dala.restserviceswagger.controller"))
.paths(PathSelectors.any())
.build();
}
}
Same for me becareful with Spring boot it will work until version 2.8.0 it does'nt work with version 3.0.0对我来说同样小心使用 Spring boot 它会一直工作到 2.8.0 版本它不适用于版本 3.0.0
tested with this用这个测试
@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Value("${application.version}")
private String version;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.langton"))
.paths(PathSelectors.any()).build();
}
/**
* will add some importants information to the swagger docs
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfo("Langton ant app", "rest api for langton ant app", version, null,
new Contact("name", "N/A", "email"), null, null, Collections.EMPTY_LIST);
}
}
and in main class和在主班
@SpringBootApplication
public class LangtonAntLauncher{
public static void main(String[] args) {
SpringApplication.run(LangtonAntLauncher.class, args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.