We are building a new Rest service. I have a PUT method that looks something like this:
@RestController
public class RestController...
.
.
@PutMapping("/api/entity1")
public ResponseEntity<String> entity1Put(@RequestBody Myclass entity,
@RequestParam(value = "node1", defaultValue = emptyString) String node1,
@RequestParam(value = "user", defaultValue = emptyString) String user,
@RequestParam(value = "password", defaultValue = emptyString) String password) {...}
The method is essentially a proxy for calling the business layer of the app. My POST method will have exactly the same signature, preparation code and return type. The only difference will be the name of the method called at the business layer. In order to have less code and promote API consistency, I would like to have a single method that handles both PUT and POST requests.
Is it possible to do this?
@RestController
public class RestController...
.
.
@PutMapping("/api/entity1")
@PostMapping("/api/entity1")
public ResponseEntity<String> entity1PutandPost(@RequestBody Myclass entity,
@RequestParam(value = "node1", defaultValue = emptyString) String node1,
@RequestParam(value = "user", defaultValue = emptyString) String user,
@RequestParam(value = "password", defaultValue = emptyString) String password) {...}
If so, then how can I determine the request's HTTP method?