I have a REST endpoint in my Spring Boot application (version 1.5.9.RELEASE) with the following signature:
@RequestMapping(value = "/users", method = RequestMethod.POST) throws UsernameExistsException, EmailExistsException
public UserProxy addUser(@RequestBody UserProxy userProxy) {
...
}
The exceptions are handled by an @ExceptionHandler
annotated method in a @ControllerAdvice
annotated class:
@EnableWebMvc
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(EmailExistsException.class)
public ResponseEntity<String> handleEmailExistsException() {
return ResponseEntity
.status(UsersStatusCodes.EMAIL_EXISTS) // status code 912
.body("Email already exists");
}
@ExceptionHandler(UsernameExistsException.class)
public ResponseEntity<String> handleUsernameExistsException() {
return ResponseEntity
.status(UsersStatusCodes.USERNAME_EXISTS) // status code 911
.body("Username already exists");
}
}
However, when the endpoint is accessed and an exception is thrown, I retrieve the following exception on the client side:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://192.168.99.100:8080/users": stream is closed; nested exception is java.io.IOException: stream is closed
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:673)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:387)
at com.cheetahrunner.users.mongo.client.UsersClient.createUser(UsersClient.java:44)
at com.cheetahrunner.users.UsersServiceTest.addUser(UsersServiceTest.java:102)
at com.cheetahrunner.users.UsersServiceTest.testAddRemoveUser(UsersServiceTest.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
15:37:44.517 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://192.168.99.100:8080/users" resulted in 911 (null)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: java.io.IOException: stream is closed
at java.base/sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(HttpURLConnection.java:3417)
at java.base/sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3422)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:83)
at java.base/java.io.PushbackInputStream.read(PushbackInputStream.java:136)
at org.springframework.web.client.MessageBodyClientHttpResponseWrapper.hasEmptyMessageBody(MessageBodyClientHttpResponseWrapper.java:102)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:82)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:662)
... 30 more
The code used to access the endpoint is the following:
rest.exchange(host + "/users", HttpMethod.POST, new HttpEntity<UserProxy>(user), UserProxy.class)
I got no idea what could cause the problem, does anyone have a hint for me?
The endpoint is reachable via Postman, where 911 response with status text "Username already exists" is received.