@PutMapping: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(→REST) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[Spring MVC Concepts#.40PutMapping|Spring MVC Concepts]] | * [[Spring MVC Concepts#.40PutMapping|Spring MVC Concepts]] | ||
* [[Spring_REST_Concepts#Update_a_Resource|Spring REST Concepts]] | |||
=Overview= | =Overview= | ||
The annotation may also contain a sub-path relative to the class-level base path, usually configured with [[@RequestMapping]]. | The annotation may also contain a sub-path relative to the class-level base path, usually configured with [[@RequestMapping]]. | ||
=REST= | |||
<syntaxhighlight lang='java'> | |||
@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); | |||
} | |||
} | |||
</syntaxhighlight> | |||
For possible problems related to [[@RequestBody]] and deserialization from JSON, see: {{Internal|Spring_REST_Concepts#Request_Body_and_Jackson_Deserialization|Request Body and Jackson Deserialization}} | |||
==HTTP POST Semantics for REST Applications== | |||
{{Internal|REST_and_Hypermedia#POST|HTTP POST Semantics for REST Applications}} | |||
==HTTP PUT Semantics for REST Applications== | |||
{{Internal|REST_and_Hypermedia#PUT|HTTP PUT Semantics for REST Applications}} |
Latest revision as of 16:00, 27 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);
}
}
For possible problems related to @RequestBody and deserialization from JSON, see: