@PostMapping: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=


* [[Spring_MVC_Concepts#Controller|Spring MVC Concepts]]
* [[Spring MVC Concepts#.40PostMapping|Spring MVC Concepts]]
* [[Spring_REST_Concepts#Create_a_Resource|Spring REST Concepts]]


=Overview=
=Overview=


Designates a controller handler method to handle POST requests. The content of the HTML form posted is presented to the method as a domain model data object. In the example below, the form is used to build a taco composition:
Designates a controller handler method to handle POST requests. The annotation may also contain a sub-path relative to the class-level base path, usually configured with [[@RequestMapping]].
 
The content of the posted HTML form is presented to the method as a domain model data object: when the form is submitted, the fields in the form are bound to properties of the domain model object instance. In the example below, the form is used to build a taco composition:


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
Line 11: Line 14:
public String processFormContent(Taco taco) {
public String processFormContent(Taco taco) {


        // ...
  // ...
  return "redirect:/orders/current";
}
</syntaxhighlight>
 
=REST=
 
<syntaxhighlight lang='java'>
@RestController
@RequestMapping(path = "/a", produces = "application/json")
public class AController {
 
  @PostMapping(consumes = "application/json")
  @ResponseStatus(HttpStatus.CREATED)
  public A post(@RequestBody A a) {


        return "redirect:/orders/current";
    content.put(a.getId(), a);
    }
    return a;
  }
}
</syntaxhighlight>
</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}}
==Data Members==
===consume===
Specifies what kind of content type(s) the handler is capable of handling.

Latest revision as of 15:58, 27 March 2019

Internal

Overview

Designates a controller handler method to handle POST requests. The annotation may also contain a sub-path relative to the class-level base path, usually configured with @RequestMapping.

The content of the posted HTML form is presented to the method as a domain model data object: when the form is submitted, the fields in the form are bound to properties of the domain model object instance. In the example below, the form is used to build a taco composition:

@PostMapping
public String processFormContent(Taco taco) {

  // ...
  return "redirect:/orders/current";
}

REST

@RestController
@RequestMapping(path = "/a", produces = "application/json")
public class AController {
  
  @PostMapping(consumes = "application/json")
  @ResponseStatus(HttpStatus.CREATED)
  public A post(@RequestBody A a) {

    content.put(a.getId(), a);
    return a;
  }
}

For possible problems related to @RequestBody and deserialization from JSON, see:

Request Body and Jackson Deserialization

HTTP POST Semantics for REST Applications

HTTP POST Semantics for REST Applications

Data Members

consume

Specifies what kind of content type(s) the handler is capable of handling.