Skip to content

Commit b79d879

Browse files
committed
Merge pull request eugenp#342 from amedviediev/master
Source code for the article "Returning Custom Status Codes from Spring MVC Controllers"
2 parents ddec979 + dfc96b2 commit b79d879

4 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎spring-rest/pom.xml‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
<dependency>
3434
<groupId>org.springframework</groupId>
3535
<artifactId>spring-web</artifactId>
36-
<version>${org.springframework.version}</version>
3736
<exclusions>
3837
<exclusion>
3938
<artifactId>commons-logging</artifactId>
@@ -44,12 +43,10 @@
4443
<dependency>
4544
<groupId>org.springframework</groupId>
4645
<artifactId>spring-webmvc</artifactId>
47-
<version>${org.springframework.version}</version>
4846
</dependency>
4947
<dependency>
5048
<groupId>org.springframework</groupId>
5149
<artifactId>spring-oxm</artifactId>
52-
<version>${org.springframework.version}</version>
5350
</dependency>
5451

5552
<dependency>
@@ -229,7 +226,6 @@
229226

230227
<properties>
231228
<!-- Spring -->
232-
<org.springframework.version>4.2.4.RELEASE</org.springframework.version>
233229
<org.springframework.security.version>4.0.3.RELEASE</org.springframework.security.version>
234230

235231
<!-- persistence -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.web.controller.status;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
10+
@Controller
11+
public class ExampleController {
12+
13+
@RequestMapping(value = "/controller", method = RequestMethod.GET)
14+
@ResponseBody
15+
public ResponseEntity sendViaResponseEntity() {
16+
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
17+
}
18+
19+
@RequestMapping(value = "/exception", method = RequestMethod.GET)
20+
@ResponseBody
21+
public ResponseEntity sendViaException() {
22+
throw new ForbiddenException();
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.web.controller.status;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason="To show an example of a custom message")
7+
public class ForbiddenException extends RuntimeException {
8+
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.baeldung.web.controller.status;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.baeldung.config.WebConfig;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.test.context.web.WebAppConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
16+
import org.springframework.web.context.WebApplicationContext;
17+
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@ContextConfiguration(classes = WebConfig.class)
20+
@WebAppConfiguration
21+
public class ExampleControllerTest {
22+
23+
private MockMvc mockMvc;
24+
25+
@Autowired
26+
private WebApplicationContext webApplicationContext;
27+
28+
@Before
29+
public void setUp() {
30+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
31+
}
32+
33+
@Test
34+
public void whenGetRequestSentToController_thenReturnsStatusNotAcceptable() throws Exception {
35+
mockMvc.perform(get("/controller"))
36+
.andExpect(status().isNotAcceptable());
37+
}
38+
39+
@Test
40+
public void whenGetRequestSentToException_thenReturnsStatusForbidden() throws Exception {
41+
mockMvc.perform(get("/exception"))
42+
.andExpect(status().isForbidden());
43+
}
44+
}

0 commit comments

Comments
 (0)