Moved RequestMapping Shortcut annotations article code from spring-mvc-java to spring-mvc-basics

This commit is contained in:
Gerardo Roza 2019-07-01 18:32:49 -03:00
parent 00c4774774
commit e84ea4d4c5
6 changed files with 146 additions and 141 deletions

View File

@ -11,4 +11,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity) - [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity)
- [A Guide to the ViewResolver in Spring MVC](http://www.baeldung.com/spring-mvc-view-resolver-tutorial) - [A Guide to the ViewResolver in Spring MVC](http://www.baeldung.com/spring-mvc-view-resolver-tutorial)
- [Guide to Spring Handler Mappings](http://www.baeldung.com/spring-handler-mappings) - [Guide to Spring Handler Mappings](http://www.baeldung.com/spring-handler-mappings)
- [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml) - [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml)
- [Spring @RequestMapping New Shortcut Annotations](http://www.baeldung.com/spring-new-requestmapping-shortcuts)

View File

@ -0,0 +1,47 @@
package com.baeldung.web.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestMappingShortcutsController {
@GetMapping("/get")
public @ResponseBody ResponseEntity<String> get() {
return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}
@GetMapping("/get/{id}")
public @ResponseBody ResponseEntity<String> getById(@PathVariable String id) {
return new ResponseEntity<String>("GET Response : " + id, HttpStatus.OK);
}
@PostMapping("/post")
public @ResponseBody ResponseEntity<String> post() {
return new ResponseEntity<String>("POST Response", HttpStatus.OK);
}
@PutMapping("/put")
public @ResponseBody ResponseEntity<String> put() {
return new ResponseEntity<String>("PUT Response", HttpStatus.OK);
}
@DeleteMapping("/delete")
public @ResponseBody ResponseEntity<String> delete() {
return new ResponseEntity<String>("DELETE Response", HttpStatus.OK);
}
@PatchMapping("/patch")
public @ResponseBody ResponseEntity<String> patch() {
return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
}
}

View File

@ -0,0 +1,97 @@
package com.baeldung.web.controller;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
public class RequestMapingShortcutsIntegrationTest {
private MockMvc mockMvc;
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new RequestMappingShortcutsController())
.build();
}
@Test
public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get");
ResultMatcher contentMatcher = MockMvcResultMatchers.content()
.string("GET Response");
this.mockMvc.perform(builder)
.andExpect(contentMatcher)
.andExpect(MockMvcResultMatchers.status()
.isOk());
}
@Test
public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post");
ResultMatcher contentMatcher = MockMvcResultMatchers.content()
.string("POST Response");
this.mockMvc.perform(builder)
.andExpect(contentMatcher)
.andExpect(MockMvcResultMatchers.status()
.isOk());
}
@Test
public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put");
ResultMatcher contentMatcher = MockMvcResultMatchers.content()
.string("PUT Response");
this.mockMvc.perform(builder)
.andExpect(contentMatcher)
.andExpect(MockMvcResultMatchers.status()
.isOk());
}
@Test
public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete");
ResultMatcher contentMatcher = MockMvcResultMatchers.content()
.string("DELETE Response");
this.mockMvc.perform(builder)
.andExpect(contentMatcher)
.andExpect(MockMvcResultMatchers.status()
.isOk());
}
@Test
public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch");
ResultMatcher contentMatcher = MockMvcResultMatchers.content()
.string("PATCH Response");
this.mockMvc.perform(builder)
.andExpect(contentMatcher)
.andExpect(MockMvcResultMatchers.status()
.isOk());
}
}

View File

@ -15,7 +15,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [File Upload with Spring MVC](http://www.baeldung.com/spring-file-upload) - [File Upload with Spring MVC](http://www.baeldung.com/spring-file-upload)
- [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring) - [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring)
- [Introduction to HtmlUnit](http://www.baeldung.com/htmlunit) - [Introduction to HtmlUnit](http://www.baeldung.com/htmlunit)
- [Spring @RequestMapping New Shortcut Annotations](http://www.baeldung.com/spring-new-requestmapping-shortcuts)
- [Upload and Display Excel Files with Spring MVC](http://www.baeldung.com/spring-mvc-excel-files) - [Upload and Display Excel Files with Spring MVC](http://www.baeldung.com/spring-mvc-excel-files)
- [Spring MVC Custom Validation](http://www.baeldung.com/spring-mvc-custom-validator) - [Spring MVC Custom Validation](http://www.baeldung.com/spring-mvc-custom-validator)
- [web.xml vs Initializer with Spring](http://www.baeldung.com/spring-xml-vs-java-config) - [web.xml vs Initializer with Spring](http://www.baeldung.com/spring-xml-vs-java-config)

View File

@ -1,47 +0,0 @@
package com.baeldung.web.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestMappingShortcutsController {
@GetMapping("/get")
public @ResponseBody ResponseEntity<String> get() {
return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}
@GetMapping("/get/{id}")
public @ResponseBody ResponseEntity<String> getById(@PathVariable String id) {
return new ResponseEntity<String>("GET Response : " + id, HttpStatus.OK);
}
@PostMapping("/post")
public @ResponseBody ResponseEntity<String> post() {
return new ResponseEntity<String>("POST Response", HttpStatus.OK);
}
@PutMapping("/put")
public @ResponseBody ResponseEntity<String> put() {
return new ResponseEntity<String>("PUT Response", HttpStatus.OK);
}
@DeleteMapping("/delete")
public @ResponseBody ResponseEntity<String> delete() {
return new ResponseEntity<String>("DELETE Response", HttpStatus.OK);
}
@PatchMapping("/patch")
public @ResponseBody ResponseEntity<String> patch() {
return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
}
}

View File

@ -1,92 +0,0 @@
package com.baeldung.web.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.spring.web.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class)
public class RequestMapingShortcutsIntegrationTest {
@Autowired
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setup () {
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.ctx);
this.mockMvc = builder.build();
}
@Test
public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("GET Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PUT Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch");
ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PATCH Response");
this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
}