1

I'm trying to make documentation on the code I'm working with using Spring-boot , swagger2 and H2-database.

Here is the 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.

    <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. There I get: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Feb 14 15:22:38 CET 2019 There was an unexpected error (type=Not Found, status=404). No message available

I don't have any Spring security or anything.

David Larsson
  • 13
  • 1
  • 4

2 Answers2

2

Use SwaggerConfig without extending WebMvcConfigurationSupport should work then.

Error is not related to security its telling that there is no mapped path with that 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();
 }
}
Nenad Radak
  • 3,550
  • 2
  • 27
  • 36
  • 2
    It doesn't work even with this solution in my case. – Alessandro C Jul 01 '19 at 08:24
  • @AlessandroC Same here, but ultimately I fixed it by using the two springfox maven dependencies in OP, including their VERSIONS. Max version that works for me is 2.7.0. (and ofcourse like the solution says, don't extend WebMvcConfigurationSupport). – Albert Hendriks Aug 12 '20 at 12:23
0

Same for me becareful with Spring boot it will work until version 2.8.0 it does'nt work with version 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);
}

}
cyril
  • 872
  • 6
  • 29