Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest: Override Method in RestController with same request-mapping-path

Given the following working repository in our application:

public interface PersonRepository extends PagingAndSortingRepository<Person, Integer> {

}

The Repository is exposed via spring-data-rest with URI "/api/persons" and works as expected.

We now want to override the post-method of the repository in a method of a RestController:

@RestController
@RequestMapping("/persons")
public class PersonController {

@RequestMapping(value = "/**", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> savePerson(@RequestBody Person person) {
      //do something fancy
      return "it works";
}

If we post data to "/api/persons" the method of the PersonController is called but none of the methods of the PersonRepository (e.g. GET) can be accessed via rest. We constantly get an 405 error and the following exception:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

After some playing around we found out that everything works as expected (methods of repository and controller can be called) if we change the value-property of the @RequestMapping annotation from

value="/**"

to

value="/save"

After reading this question and the linked documenation it should also work if the value-property is "/**"

like image 883
CSan Avatar asked Oct 31 '22 10:10

CSan


1 Answers

Finally, after upgrading to new versions of spring/spring-data/spring-data-rest everything works as expected.

like image 71
CSan Avatar answered Nov 11 '22 02:11

CSan