Search code examples
javaspringspring-bootspring-data-restspring-restcontroller

Intermittent RestRepositoryController 404 issue with Spring Boot 1.5.10?


I am experiencing a strange issue with Spring boot and one controller.

I believe it is due to a conflict with another controller which is similarly named (a newer version)

The structure is like so

 controllers
    -v2
       -PartController(@RestController)
    -v1
       -PartController(@RepositoryRestController)
 repositories
    -v2
       -PartRepository(@Repository)
    -v1
       -PartRepository(@RepositoryRestResource(path = "/part"))

The reason for this, is that we are introducing a service layer and want to move away from Restful repositories. Therefore v1 will be removed eventually.

However when starting the service, the original repository methods are not mapped sometimes (non deterministic). Other times it works, when checking swagger. This is the correct result when viewing on swagger

Correct endpoints

enter image description here

Incorrect endpoints (jpa base methods)

enter image description here

v1 PartController

@RepositoryRestController
public class PartController {

... more code

v1 PartRepository

@CrossOrigin
@RepositoryRestResource(path = "/part")
public interface PartRepository extends JpaRepository<Part, Long> {

... more code

v2 PartController

@RestController("PartController2")
@RequestMapping(path = "/api/v2", name = "PartController2")
public class PartController implements PartsApi {

@RequestMapping(
      method = RequestMethod.POST,
      path = "/parts/usage/")
    @Override
    public List<PartResponse> getPartUsage(@RequestBody final                          List<PartUsageRequest> request) {
    return this.service.getPartUsage(request);
  }

v2 PartRepository

@Repository("PartRepository2")
public interface PartRepository extends CrudRepository<Part, Long> {

  Optional<Part> findById(Long id);

}

Note the new endpoint is working irrespective of the other one

enter image description here

As mentioned above, this availability of the endpoints is random. Could it be a race condition when Spring boot maps each controller at startup?

It is completely random, re-running or maven cleaning the service can work as intended. Then starting it up again those endpoints on /parts are not available causing a 404

Spring Version: 1.5.10.RELEASE

Springfox (Swagger): 2.8.0


Solution

  • I have managed to reproduce your issue in a proof-of-concept project. It is because you have more than one repository for the same entity (in your case Part).

    See this change request for further details. The only difference is that you have @Repository not @RepositoryRestResource(exported = false) on the second repository but it doesn't matter, the outcome is the same: the resource is exported in about 50% of the times.

    The only workaround I see: get rid of the second repository.

    This question is also interesting.