Skip to content

Commit b57cf0c

Browse files
AbhinabKanrarmaibin
authored andcommitted
spring requestmapping shortcuts (eugenp#1096)
* rest with spark java * 4 * Update Application.java * indentation changes * spring @RequestMapping shortcuts * removing spring requestmapping and pushing spring-mvc-java
1 parent 791142c commit b57cf0c

3 files changed

Lines changed: 143 additions & 3 deletions

File tree

spring-mobile/src/main/java/com/baeldung/Application.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
@SpringBootApplication
77
public class Application {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(Application.class, args);
11-
}
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
12+
}
1213

1314
}
1415

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.DeleteMapping;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PatchMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.PutMapping;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
public class RequestMappingShortcutsController {
16+
17+
@GetMapping("/get")
18+
public @ResponseBody ResponseEntity<String> get() {
19+
return new ResponseEntity<String>("GET Response", HttpStatus.OK);
20+
}
21+
22+
@GetMapping("/get/{id}")
23+
public @ResponseBody ResponseEntity<String> getById(@PathVariable String id) {
24+
return new ResponseEntity<String>("GET Response : " + id, HttpStatus.OK);
25+
}
26+
27+
@PostMapping("/post")
28+
public @ResponseBody ResponseEntity<String> post() {
29+
return new ResponseEntity<String>("POST Response", HttpStatus.OK);
30+
}
31+
32+
@PutMapping("/put")
33+
public @ResponseBody ResponseEntity<String> put() {
34+
return new ResponseEntity<String>("PUT Response", HttpStatus.OK);
35+
}
36+
37+
@DeleteMapping("/delete")
38+
public @ResponseBody ResponseEntity<String> delete() {
39+
return new ResponseEntity<String>("DELETE Response", HttpStatus.OK);
40+
}
41+
42+
@PatchMapping("/patch")
43+
public @ResponseBody ResponseEntity<String> patch() {
44+
return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
45+
}
46+
47+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
import org.springframework.test.context.web.WebAppConfiguration;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.ResultMatcher;
12+
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
13+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
15+
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
16+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
17+
import org.springframework.web.context.WebApplicationContext;
18+
19+
import com.baeldung.spring.web.config.WebConfig;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@WebAppConfiguration
23+
@ContextConfiguration(classes = WebConfig.class)
24+
public class RequestMapingShortcutsUnitTest {
25+
26+
@Autowired
27+
private WebApplicationContext ctx;
28+
29+
private MockMvc mockMvc;
30+
31+
@Before
32+
public void setup () {
33+
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.ctx);
34+
this.mockMvc = builder.build();
35+
}
36+
37+
@Test
38+
public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception {
39+
40+
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get");
41+
42+
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("GET Response");
43+
44+
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
45+
46+
}
47+
48+
@Test
49+
public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception {
50+
51+
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post");
52+
53+
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response");
54+
55+
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
56+
57+
}
58+
59+
@Test
60+
public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception {
61+
62+
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put");
63+
64+
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PUT Response");
65+
66+
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
67+
68+
}
69+
70+
@Test
71+
public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception {
72+
73+
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete");
74+
75+
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response");
76+
77+
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
78+
79+
}
80+
81+
@Test
82+
public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception {
83+
84+
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch");
85+
86+
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PATCH Response");
87+
88+
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
89+
90+
}
91+
92+
}

0 commit comments

Comments
 (0)