Swagger UI не отображается из-за My SpringBoot Applcation, каждая конечная точка ожидает 2 параметра запроса - PullRequest
0 голосов
/ 27 января 2020

Загрузочное приложение My Spring, все конечные точки ожидают 2 параметра запроса (http://localhost:myapplcation/getAll?pid=1101&token=jdu191092kj11), все конечные точки работают должным образом. Но если я передаю 2 параметра запроса, я вижу api swagger (http://localhost:8080/v2/api-docs?pid=1101&token=jdu191092kj11)

Проблема с этим интерфейсом Swagger .. ничего не отображается http://localhost:8080/swagger-ui.html

как я могу решить эту проблему?

Ниже приведен код:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springframework.controllers"))
                .paths(regex("/product.*"))
                .build()
                .apiInfo(metaData());
    }
    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("Spring Boot REST API")
                .description("\"Spring Boot REST API for Online Store\"")
                .version("1.0.0")
                .license("Apache License Version 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
                .contact(new Contact("John Thompson", "https://springframework.com", "john@springfrmework.com"))
                .build();
    }
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

@ RestController publi c class EmployeeController {

@Autowired
private EmployeeService employeeService;

@PostMapping("/admin/empproperties")
public @ResponseBody ResponseEntity<Employee> addNeeEmp(@RequestParam String fname, @RequestParam String sname) {
    Employee emp = new Employee();
    emp.setParam(fname);
    emp.setValue(sname);
    emp = employeeService.save(emp);
    return new ResponseEntity<Employee>(emp, new HttpHeaders(), HttpStatus.OK);
}


@GetMapping("/admin/empproperties")
public ResponseEntity<List<Employee>> allEmployees(){

    List<Employee> list = employeeService.findAll();
    return new ResponseEntity<List<Employee>>(list, new HttpHeaders(), HttpStatus.OK);

}

}

1 Ответ

0 голосов
/ 27 января 2020

В регулярном выражении вы добавляете товар. * Но в пути URL у вас нет термина продукта.

Давайте просто и посмотрим, как это работает, как показано ниже

 @Bean
    public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(metaData())
         .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
        .paths(PathSelectors.any())
        .build();
    }
...