@PutMapping: Difference between revisions
Jump to navigation
Jump to search
(→REST) |
(→REST) |
||
Line 14: | Line 14: | ||
@RequestMapping(path = "/a", produces = "application/json") | @RequestMapping(path = "/a", produces = "application/json") | ||
public class AController { | public class AController { | ||
@PutMapping(path = "/{id}", consumes = "application/json") | |||
public ResponseEntity<A> put(@PathVariable("id") Integer id, @RequestBody A a) { | |||
// wholesale replacement, make sure there is such an ID and the A instance is valid | |||
A old = content.get(id); | |||
if (old == null) { | |||
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND); | |||
} | |||
old.updateFrom(a); | |||
return new ResponseEntity<>(old, HttpStatus.OK); | |||
} | |||
} | |||
@PutMapping(consumes = "application/json") | @PutMapping(consumes = "application/json") |
Revision as of 02:21, 13 March 2019
Internal
Overview
The annotation may also contain a sub-path relative to the class-level base path, usually configured with @RequestMapping.
REST
@RestController
@RequestMapping(path = "/a", produces = "application/json")
public class AController {
@PutMapping(path = "/{id}", consumes = "application/json")
public ResponseEntity<A> put(@PathVariable("id") Integer id, @RequestBody A a) {
// wholesale replacement, make sure there is such an ID and the A instance is valid
A old = content.get(id);
if (old == null) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
old.updateFrom(a);
return new ResponseEntity<>(old, HttpStatus.OK);
}
}
@PutMapping(consumes = "application/json")
public A put(@RequestBody A a) {
// wholesale replacement, make sure the A instance is valid
...
}
}