Reputation: 1724
I have a spring boot application runs on jetty + swagger and I don't see any end point specification when i go to http://localhost:8080/home/swagger-ui.html (see attachment) even do I wrote a controller and have an endpoint there.
my Application code is:
package com.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GcmAuthenticationServiceApplication.class, args);
}
}
in pom.xml I use:
<!-- Swagger 2.0 support -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
my controller:
package com.MyService.controller;
import com.MyService.model.RegistrationData;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
@Api(value = "register", description = "the registration API")
public interface RegistrationApi {
@ApiOperation(value = "Register user", nickname = "register", notes = "", authorizations = {
@Authorization(value = "petstore_auth", scopes = {
@AuthorizationScope(scope = "write:pets", description = "modify pets in your account"),
@AuthorizationScope(scope = "read:pets", description = "read your pets")
})
}, tags={ "register", })
@ApiResponses(value = {
@ApiResponse(code = 405, message = "Invalid input") })
@RequestMapping(value = "/register",
produces = { "application/json", "application/xml" },
consumes = { "application/json", "application/xml" },
method = RequestMethod.POST)
default ResponseEntity<Void> registerUser(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )
@Valid @RequestBody RegistrationData body) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
------
package com.MyService.controller;
import com.MyService.model.RegistrationData;
import com.MyService.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1")
public class RegistrationController implements RegistrationApi{
private RegistrationService registrationService;
@Autowired
public void setProductService(RegistrationService registrationService) {
this.registrationService = registrationService;
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ResponseEntity registerUser(@RequestBody RegistrationData registrationData){
//todo call service to rgeister
return new ResponseEntity("user register successfully", HttpStatus.OK);
}
}
my SwaggerConfig:
package com.MyService.config;
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.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;
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("guru.springframework.controllers"))
.paths(regex("/product.*")).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/");
}
}
my swagger documentation look as the following:
Upvotes: 1
Views: 11193
Reputation: 872
You can use the below with a change to your existing code. The base package must point to your controller base package and the URL context should be among the one's which you have added in the controller.
Modified base package to "com.MyService.controller" and added "/api/v1" & "/register" to the paths regex,
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.MyService.controller"))
.paths(regex("(/api/v1.*)|(/register.*)")).build();
}
Upvotes: 2
Reputation: 356
You are allowed to show /product.*
URLs but In your endpoint, there don't have any URL match with /product.*
. that's why it doesn't show any endpoint.
change your code like below
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("guru.springframework.controllers"))
.paths(or(regex("/register.*"),regex("/api/v1.*"))).build();
}
you can allow all endpoints using regex("/.*")
.
Upvotes: 0