-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathIndexController.java
More file actions
135 lines (117 loc) · 4.6 KB
/
IndexController.java
File metadata and controls
135 lines (117 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package guru.springframework.blog.controllers;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.*;
@RestController
@RequestMapping("/home")
public class IndexController {
/*@RequestMapping at method level*/
@RequestMapping("/index")
String index(){
return "Hello from index";
}
@RequestMapping(value={"", "/page", "page*","view/*"})
String indexMultipleMapping(){
return "Hello from index multiple mapping.";
}
/*@RequestMapping using HTTP request methods*/
@RequestMapping(method = RequestMethod.GET)
String get(){
return "Hello from get";
}
@RequestMapping(method = RequestMethod.DELETE)
String delete(){
return "Hello from delete";
}
@RequestMapping(method = RequestMethod.POST)
String post(){
return "Hello from post";
}
@RequestMapping(method = RequestMethod.PUT)
String put(){
return "Hello from put";
}
@RequestMapping(method = RequestMethod.PATCH)
String patch(){
return "Hello from patch";
}
/*@RequestMapping with headers*/
@RequestMapping(value = "/head", headers = {"content-type=text/plain", "content-type=text/html"})
String getWithHeaders(){
return "Mapping applied along with headers";
}
/*@RequestMapping with @RequestParam*/
@RequestMapping(value = "/personId")
String getId(@RequestParam String personId){
System.out.println("ID is "+personId);
return "Get ID from query string of URL without value element";
}
@RequestMapping(value = "/id")
String getIdByValue(@RequestParam("id") String personId){
System.out.println("ID is "+personId);
return "Get ID from query string of URL with value element";
}
@RequestMapping(value = "/name")
String getName(@RequestParam(value = "person", required = false) String personName ){
return "Required element of request param";
}
/*@RequestMapping with produces and consumes attributes*/
@RequestMapping(value = "/prod", produces = {"text/html", "application/JSON"})
@ResponseBody
String getProduces(){
return "Produces attribute";
}
@RequestMapping(value = "/cons", consumes = {"text/plain", "application/XML"})
String getConsumes(){
return "Consumes attribute";
}
/*@RequestMapping with @PathVariable for Dynamic URI */
@RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
String getDynamicUriValue(@PathVariable String id){
System.out.println("ID is "+id);
return "Dynamic URI parameter fetched";
}
@RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
String getDynamicUriValueRegex(@PathVariable("name") String name){
System.out.println("Name is "+name);
return "Dynamic URI parameter fetched using regex";
}
/*@RequestMapping with params attribute*/
@RequestMapping(value = "/fetch", params = {"personId=10"})
String getParams(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = "+id;
}
@RequestMapping(value = "/fetch", params = {"personId=20"})
String getParamsDifferent(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = "+id;
}
/*@RequestMapping for default method*/
@RequestMapping()
String defaultMethod(){
return "This is a default method for the class";
}
/*@RequestMapping shortcuts*/
@GetMapping("/person")
public @ResponseBody ResponseEntity<String> getPerson() {
return new ResponseEntity<String>("Response from GET", HttpStatus.OK);
}
@GetMapping("/person/{id}")
public @ResponseBody ResponseEntity<String> getPersonById(@PathVariable String id){
return new ResponseEntity<String>("Response from GET with id " + id, HttpStatus.OK);
}
@PostMapping("/person/post")
public @ResponseBody ResponseEntity<String> postPerson() {
return new ResponseEntity<String>("Response from POST method", HttpStatus.OK);
}
@PutMapping("/person/put")
public @ResponseBody ResponseEntity<String> putPerson() {
return new ResponseEntity<String>("Response from PUT method", HttpStatus.OK);
}
@DeleteMapping("/person/delete")
public @ResponseBody ResponseEntity<String> deletePerson() {
return new ResponseEntity<String>("Response from DELETE method", HttpStatus.OK);
}
@PatchMapping("/person/patch")
public @ResponseBody ResponseEntity<String> patchPerson() {
return new ResponseEntity<String>("Response from PATCH method", HttpStatus.OK);
}
}