2

I'm using Spring Boot 1.5.3, Spring Data REST, Hibernate, Swagger. Spring Data REST is great but sometimes I need to add custom operations to a specific endpoint. Unfortunately this question doesn't help.

My typical repository is:

@Transactional
@PreAuthorize("isAuthenticated()")
public interface WorkSessionRepository extends PagingAndSortingRepository<WorkSession, Long> {}

Let's say I've my custom controller:

@RepositoryRestController
@RequestMapping(path = "/api/v1/workSessions")
public class WorkSessionController {

    @Autowired
    private EntityLinks entityLinks;

    @Autowired
    private WorkSessionRepository workSessionRepository;

    @Autowired
    private UserRepository userRepository;

    @PreAuthorize("isAuthenticated()")
    @RequestMapping(method = RequestMethod.POST, path = "/start")
    public ResponseEntity<?> start(@RequestBody(required = true) CheckPoint checkPoint) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    ....
    ....
    Resource<WorkSession> resource = new Resource<>(workSession); 
    resource.add(entityLinks.linkFor(WorkSession.class).slash(workSession.getId()).withSelfRel());
    return ResponseEntity.ok(resource);

Now Swagger2 show two different controllers:

swagger view

I would like my methods is added in the WorkSessionEntity.

This is the configuration of Swagger:

@Configuration
@EnableSwagger2
@Import({ springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class,
        springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class })
@ComponentScan(basePackageClasses = { BusletApplication.class })
public class SwaggerConfig {

    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket internalAngularApi() {
        return new Docket(DocumentationType.SPRING_WEB)// Enable swagger2
                .select().//
                apis(RequestHandlerSelectors.any())// what list in the doc
                .paths(paths()).build()// select which paths shows
                .pathMapping("/").// the api base path
                directModelSubstitute(LocalDate.class, String.class).// replace
                                                                        // specific
                                                                        // model
                genericModelSubstitutes(ResponseEntity.class)// generic
                                                                // replacement
                .alternateTypeRules(
                        newRule(typeResolver.resolve(DeferredResult.class, typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                                typeResolver.resolve(WildcardType.class)))
                .useDefaultResponseMessages(false).//
                enableUrlTemplating(false).apiInfo(apiInfo()).groupName("Internal API for Angular UI");//
    }

    // Here is an example where we select any api that matches one of these
    // paths
    private Predicate<String> paths() {
        return or(regex("/api/v1/.*"), regex("/error/.*"));
    }

    @Bean
    public UiConfiguration uiConfig() {
        return new UiConfiguration("validatorUrl", // url
                "none", // docExpansion => none | list
                "alpha", // apiSorter => alpha
                "schema", // defaultModelRendering => schema
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, //
                false, // enableJsonEditor
                true, // showRequestHeaders => true | false
                6000L); // requestTimeout => in milliseconds
    }

Is there a way to integrate custom controllers with repository created by Spring Data REST in a graceful way?

drenda
  • 5,846
  • 11
  • 68
  • 141

1 Answers1

1

Use @Api(tags= " WorkSession Entity") in your WorkSessionController.

You can find the io.swagger.annotations.Api in the swagger-annotations-1.5.10.jar.

Alejandro
  • 7,290
  • 4
  • 34
  • 59
Willy
  • 26
  • 1