我正在尝试使用Spring-boot,swagger2和H2数据库编写有关正在使用的代码的文档。
这是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();
}
}
这是控制器
@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安全性或其他任何功能。
答案 0 :(得分:1)
使用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();
}
}
答案 1 :(得分:0)
对我而言,请注意Spring Boot,直到2.8.0版 它不适用于3.0.0版
对此进行了测试
@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);
}
}
在主要班级
@SpringBootApplication
public class LangtonAntLauncher{
public static void main(String[] args) {
SpringApplication.run(LangtonAntLauncher.class, args);
}
}