* Added example and test for "using @ResponseStatus" article in the spring-mvc-basics module

This commit is contained in:
Gerardo Roza 2019-05-26 17:42:54 -03:00
parent 33d0ac5738
commit ee4953b7ef
4 changed files with 101 additions and 1 deletions

View File

@ -8,3 +8,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles:
- [Spring MVC Tutorial](https://www.baeldung.com/spring-mvc-tutorial)
- [The Spring @Controller and @RestController Annotations](http://www.baeldung.com/spring-controller-vs-restcontroller)
- [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity)

View File

@ -0,0 +1,48 @@
package com.baeldung.web.controller;
import java.util.concurrent.ThreadLocalRandom;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.model.Book;
@RestController
public class ResponseStatusRestController {
@GetMapping("/teapot")
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
public void teaPot() {
}
@GetMapping("empty")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void emptyResponse() {
}
@GetMapping("empty-no-responsestatus")
public void emptyResponseWithoutResponseStatus() {
}
@PostMapping("create")
@ResponseStatus(HttpStatus.CREATED)
public Book createEntity() {
// here we would create and persist an entity
int randomInt = ThreadLocalRandom.current()
.nextInt(1, 100);
Book entity = new Book(randomInt, "author" + randomInt, "title" + randomInt);
return entity;
}
@PostMapping("create-no-responsestatus")
public Book createEntityWithoutResponseStatus() {
// here we would create and persist an entity
int randomInt = ThreadLocalRandom.current()
.nextInt(1, 100);
Book entity = new Book(randomInt, "author" + randomInt, "title" + randomInt);
return entity;
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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.setup.MockMvcBuilders;
public class ResponseStatusRestControllerIntegrationTest {
private MockMvc mockMvc;
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new ResponseStatusRestController())
.build();
}
@Test
public void whenTeapotEndpointCalled_thenTeapotResponseObtained() throws Exception {
this.mockMvc.perform(get("/teapot"))
.andExpect(status().isIAmATeapot());
}
@Test
public void whenEmptyNoContentEndpointCalled_thenNoContentResponseObtained() throws Exception {
this.mockMvc.perform(get("/empty"))
.andExpect(status().isNoContent());
}
@Test
public void whenEmptyWithoutResponseStatusEndpointCalled_then200ResponseObtained() throws Exception {
this.mockMvc.perform(get("/empty-no-responsestatus"))
.andExpect(status().isOk());
}
@Test
public void whenCreateWithCreatedEndpointCalled_thenCreatedResponseObtained() throws Exception {
this.mockMvc.perform(post("/create"))
.andExpect(status().isCreated());
}
@Test
public void whenCreateWithoutResponseStatusEndpointCalled_thenCreatedResponseObtained() throws Exception {
this.mockMvc.perform(post("/create-no-responsestatus"))
.andExpect(status().isOk());
}
}

View File

@ -26,7 +26,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Spring MVC and the @ModelAttribute Annotation](http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation)
- [Spring MVC @PathVariable with a dot (.) gets truncated](http://www.baeldung.com/spring-mvc-pathvariable-dot)
- [A Quick Example of Spring Websockets @SendToUser Annotation](http://www.baeldung.com/spring-websockets-sendtouser)
- [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity)
- [Using Spring @ResponseStatus to Set HTTP Status Code](http://www.baeldung.com/spring-response-status)
- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters)
- [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml)