2

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.

user2035039
  • 961
  • 3
  • 16
  • 30

2 Answers2

0

Implement org.springframework.web.client.ResponseErrorHandler and set it to your rest template:

rest.setErrorHandler(errorHandler);

ResponseErrorHandler has 2 methods:

/**
     * Indicate whether the given response has any errors.
     * <p>Implementations will typically inspect the
     * {@link ClientHttpResponse#getStatusCode() HttpStatus} of the response.
     * @param response the response to inspect
     * @return {@code true} if the response has an error; {@code false} otherwise
     * @throws IOException in case of I/O errors
     */
    boolean hasError(ClientHttpResponse response) throws IOException;

    /**
     * Handle the error in the given response.
     * <p>This method is only called when {@link #hasError(ClientHttpResponse)}
     * has returned {@code true}.
     * @param response the response with the error
     * @throws IOException in case of I/O errors
     */
    void handleError(ClientHttpResponse response) throws IOException;

In the second one you should implement error handling logic.

The first one should be like:

@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
    HttpStatus code = response.getStatusCode();
    return code != HttpStatus.OK && code != HttpStatus.NO_CONTENT && code != HttpStatus.NOT_MODIFIED;
}
Andrew Nepogoda
  • 1,825
  • 17
  • 24
0

I experienced the same issue (with the same version of Spring Boot) and to fix it I removed the content and set a content length header of 0:

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<Object> applicationFormNotFound() {
    final HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    headers.add(HttpHeaders.CONTENT_LENGTH, "0");
    return ResponseEntity.status(HttpStatus.NOT_FOUND)
            .headers(headers)
            .build();
}

Admittedly this is not much help if you actually need to send content!

Dave Bower
  • 3,487
  • 26
  • 28