Skip to content

Commit 10c3bf3

Browse files
ueberfuhrRalf Ueberfuhr
andauthored
BAEL-8372: Add samples for content negotiation in error handling. (#18410)
Co-authored-by: Ralf Ueberfuhr <[email protected]>
1 parent 1fbdcd7 commit 10c3bf3

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed

spring-boot-rest/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<dependency>
120120
<groupId>net.sourceforge.htmlunit</groupId>
121121
<artifactId>htmlunit</artifactId>
122+
<version>${htmlunit.version}</version>
122123
<scope>test</scope>
123124
<exclusions>
124125
<exclusion>
@@ -174,6 +175,11 @@
174175
<modelmapper.version>3.2.0</modelmapper.version>
175176
<rest-assured.version>5.5.0</rest-assured.version>
176177
<jaxb-runtime.version>4.0.1</jaxb-runtime.version>
177-
<spring-oxm.version>6.1.4</spring-oxm.version>
178+
<spring-oxm.version>6.2.3</spring-oxm.version>
179+
<spring-boot.version>3.4.3</spring-boot.version>
180+
<!-- https://github.com/qos-ch/logback/issues/853 -->
181+
<logback.version>1.5.17</logback.version>
182+
<!-- not managed by Spring anymore -->
183+
<htmlunit.version>2.70.0</htmlunit.version>
178184
</properties>
179185
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.web.error;
2+
3+
import com.baeldung.web.exception.CustomException3;
4+
import com.baeldung.web.exception.CustomException4;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.HttpStatusCode;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ProblemDetail;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseStatus;
11+
import org.springframework.web.bind.annotation.RestControllerAdvice;
12+
13+
@RestControllerAdvice
14+
public class MyGlobalExceptionHandler {
15+
16+
// simple example for global exception handling
17+
@ResponseStatus(HttpStatus.BAD_REQUEST)
18+
@ExceptionHandler(CustomException3.class)
19+
public void handleCustomException3() {
20+
//
21+
}
22+
23+
// content negotiation
24+
@ResponseStatus(HttpStatus.BAD_REQUEST)
25+
@ExceptionHandler(produces = MediaType.APPLICATION_JSON_VALUE)
26+
public ProblemDetail handleCustomException4Json(CustomException4 ex) {
27+
String message = "custom exception 4: " + ex.getMessage();
28+
return ProblemDetail
29+
.forStatusAndDetail(HttpStatusCode.valueOf(HttpStatus.BAD_REQUEST.value()), message);
30+
}
31+
32+
@ResponseStatus(HttpStatus.BAD_REQUEST)
33+
@ExceptionHandler(produces = MediaType.TEXT_PLAIN_VALUE)
34+
public String handleCustomException4Text(CustomException4 ex) {
35+
return "custom exception 4: " + ex.getMessage();
36+
}
37+
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.web.exception;
2+
3+
import java.io.Serial;
4+
5+
public class CustomException3 extends RuntimeException {
6+
7+
@Serial
8+
private static final long serialVersionUID = 1L;
9+
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.web.exception;
2+
3+
import java.io.Serial;
4+
5+
public class CustomException4 extends RuntimeException {
6+
7+
@Serial
8+
private static final long serialVersionUID = 1L;
9+
10+
public CustomException4(String message) {
11+
super(message);
12+
}
13+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.web;
2+
3+
import com.baeldung.persistence.service.IFooService;
4+
import com.baeldung.web.controller.FooController;
5+
import com.baeldung.web.exception.CustomException3;
6+
import com.baeldung.web.exception.CustomException4;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11+
import org.springframework.context.ApplicationEventPublisher;
12+
import org.springframework.http.MediaType;
13+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
import org.springframework.test.web.servlet.MockMvc;
16+
17+
import static org.mockito.Mockito.when;
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
21+
22+
/**
23+
*
24+
* We'll start only the web layer.
25+
*
26+
*/
27+
@RunWith(SpringRunner.class)
28+
@WebMvcTest(FooController.class)
29+
public class GlobalExceptionHandlerIntegrationTest {
30+
31+
@Autowired
32+
private MockMvc mockMvc;
33+
34+
@MockitoBean
35+
private IFooService service;
36+
37+
@MockitoBean
38+
private ApplicationEventPublisher publisher;
39+
40+
@Test
41+
public void delete_forException3_fromService() throws Exception {
42+
when(service.findAll())
43+
.thenThrow(new CustomException3());
44+
this.mockMvc
45+
.perform(get("/foos"))
46+
.andExpect(status().isBadRequest());
47+
}
48+
49+
@Test
50+
public void delete_forException4Json_fromService() throws Exception {
51+
when(service.findAll())
52+
.thenThrow(new CustomException4("TEST"));
53+
this.mockMvc
54+
.perform(get("/foos").accept(MediaType.APPLICATION_JSON))
55+
.andExpect(status().isBadRequest())
56+
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
57+
}
58+
59+
@Test
60+
public void delete_forException4Text_fromService() throws Exception {
61+
when(service.findAll())
62+
.thenThrow(new CustomException4("TEST"));
63+
this.mockMvc
64+
.perform(get("/foos").accept(MediaType.TEXT_PLAIN))
65+
.andExpect(status().isBadRequest())
66+
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN));
67+
}
68+
69+
}

0 commit comments

Comments
 (0)