gpt4 book ai didi

java - 将 swagger2-ui 与 spring-boot 一起使用时出现 404

转载 作者:行者123 更新时间:2023-11-30 05:46:45 25 4
gpt4 key购买 nike

我正在尝试使用 Spring-boot 、 swagger2 和 H2-database 为我正在使用的代码制作文档。

这是 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();
}
}

这是 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();
}
}

这是我的 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>

当我访问http://localhost:8080/v2/api-docs时我让它工作,但当我去 http://localhost:8080/swagger-ui.html 时就不行了。我得到: 白标错误页面 此应用程序没有/error 的显式映射,因此您将其视为后备。 2019 年欧洲中部时间 2 月 14 日星期四 15:22:38 出现意外错误(类型=未找到,状态=404)。 没有可用的消息

我没有任何 Spring 安全性或任何东西。

最佳答案

使用 SwaggerConfig 而不扩展 WebMvcConfigurationSupport 应该可以工作。

错误与安全性无关,它表明该 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();
}
}

关于java - 将 swagger2-ui 与 spring-boot 一起使用时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com