A basic RESTfule service that provides the most outgoing request Method is Get and Post.
In Get, it is common to take parameters on requests, or path parameters. Response to Json.
In Post, form data or json data are often submitted as parameters to respond to Json.
1. Get request, url parameter, return json.
Prepare a request and then respond to the object.
package com.example.demo; public class Echo { private final long id; private final String content; public Echo(long id, String content) { this.id = id; this.content = content; } public long getId() { return this.id; } public String getContent() { return this.content; } }
Prepare an EchoController (whose name can be written according to the actual situation) to receive the request, and add the @RestController annotation to it to indicate that it is a response processing class that handles RESTful requests.
Increase @RequestMapping to provide a root url for this Controller, which of course can not be written. This is written in order to better categorize the URLs of the same processing. This Controller does not need to be repeated in the corresponding URLs of other processing methods.
package com.example.demo; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ModelAttribute; @RestController @RequestMapping("/echo") public class EchoController { private static final String echoTemplate1 = "received %s!"; private static final String echoTemplate2 = "%s speak to %s \'%s\'"; private final AtomicLong counter = new AtomicLong(); @RequestMapping(value="/getter/pattern1", method=RequestMethod.GET) public Echo getterPattern1(String content) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content)); } @RequestMapping(value="/getter/pattern2", method=RequestMethod.GET) public Echo getterPattern2(@RequestParam(value="content", required=false) String alias) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, alias)); } }
The @RequestMapping annotation is added to getterPattern1 to map the specified url and the processing method. The request for the url is processed by this method. The method can be omitted. After omitting, the parameters of all Http Metho and gtterPatten1 methods are mapped to the content parameters of the url by default.
Look at getterPattern2 again. The difference is that the parameter definition uses the @RequestParam annotation to map the URL parameters and method parameters. The value of @RequesteParam is the actual parameter in the url. required indicates whether the parameter must be true, and if it is not, the URL will report an exception. Preventing exceptions increases the default value of the defaultValue specified parameter. We will find that the method parameter here is alias, which is certainly different from the parameter name of url, as long as the RequestParam annotation maps their relationship.
After running, you can access the corresponding url in the browser to see the results, I use curl to access here.
curl http://localhost:8080/echo/getter/pattern1?content=hello curl http://localhost:8080/echo/getter/pattern2?content=hello
The results obtained from the above two URLs are identical except that the id increases by itself:
{"id":6,"content":"received hello!"}
2. Get request, pass url path parameter, return json.
Add a response method to EchoController.
@RequestMapping(value="/getter/pattern3/{content}", method=RequestMethod.GET) public Echo getterPattern3(@PathVariable String content) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content)); }
As you can see, there is {content} at the end of the url definition of @RequestMapping, which indicates that this is a path parameter. The @PathVariable annotation is added to the parameter content of getterPattern3 to map the method parameters to the path parameters.
After running, access the url.
curl http://localhost:8080/echo/getter/pattern3/123456
Result:
{"id":8,"content":"received 123456!"}
3.Post request, parameter submits Json data in the way of Http body.
First, we define a submission Json corresponding object, which is defined as Message.
package com.example.demo; public class Message { private String from; private String to; private String content; public Message() {} public String getFrom() { return this.from; } public String getTo() { return this.to; } public String getContent() { return this.content; } public void setFrom(String value) { this.from = value; } public void setTo(String value) { this.to = value; } public void setContent(String value) { this.content = value; } }
Increase the response method in EchoController and complete the mapping.
@RequestMapping(value="/setter/message1", method=RequestMethod.POST) public Echo setterMessage1(@RequestBody Message message) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent())); }
In the parameters of the setterMessage1 method, @RequestBody is used to map the requested Http Body and the parameter message.
After running, use curl to submit JSON data to the server. Submitted requests should be headed with "Content-Type:application/json" to indicate the body of the request Json.
curl -i -H "Content-Type:application/json" -d "{\"from\":\"Tom\",\"to\":\"Sandy\",\"content\":\"hello buddy\"}" http://localhost:8080/echo/setter/message1
Result:
{"id":9,"content":"Tom speak to Sandy 'hello buddy'"}
4.Post request, parameters submit form data in the way of Http body.
Increase the response method in EchoController and complete the mapping.
@RequestMapping(value="/setter/message2", method=RequestMethod.POST) public Echo setterMessage2(@ModelAttribute Message message) { return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent())); }
In the parameters of the setterMessage2 method, @ModelAttribute is used to map the form data and parameter message in the requested Http Body.
After running, use curl to submit form data to the server. The request submitted should be headed with "Content-Type:application/x-www-form-urlencoded", indicating that the body of the request is form data in the form of "key1 = value1 & key2 = Value2 & Key3 = value3".
curl -i -H "Content-Type:application/x-www-form-urlencoded" -d "from=sandy&to=aissen&content=go to" http://localhost:8080/echo/setter/message2
Results:
{"id":11,"content":"sandy speak to aissen 'go to'"}
End