1

In Spring Boot 1.3.5 (Java 8) I'd like my controller to return a custom status code and status msg. By "Custom" I mean, a status code that is not in org.springframework.http.HttpStatus.

I know, I should stick with the standard codes (but I can't).

Currently my controller does a

 HttpServletResponse response

  .
  .

 response.setStatus(255)

I get the expected

 java.lang.IllegalArgumentException: No matching constant for [255]
     at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:488)
     at org.springframework.test.web.servlet.result.StatusResultMatchers.getHttpStatusSeries(StatusResultMatchers.java:139)

Note, the operation may be successful, so in that case I want to return a custom 2xx code with a response object (i.e. sendError doesn't help).

Google says this may be available in springframework 4.3, but does anyone have thoughts for implementing now (with springframework 4.2.6)?

6
  • 4.3 make sense, could you use sendError instead? Commented Jul 25, 2016 at 18:14
  • 1
    Can you provide full stacktrace of your exception? Commented Jul 25, 2016 at 18:17
  • sendError wont for me because sometimes the code is for a successful operation and I need to return a response object (will update the question). Commented Jul 25, 2016 at 18:30
  • added relevant lines from stack trace Commented Jul 25, 2016 at 18:34
  • try to return a ResponseEntity Commented Jul 25, 2016 at 18:51

1 Answer 1

2

Custom http codes work on 4.2. This is the matcher in you junit that fails to assert the response code. Should work on your browser.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.