3

I want to do this instead of a null pointer exception in a java method return new ResponseEntity<String>(HttpStatus.BAD_REQUEST); but my method returns an int not a string. The main thing I want to do is return a 400 Status code. What can I do?

I tried throw new IllegalArgumentException(arg + "is not a valid argument; positive value expected"); but that returns a 500 status code with a good message. I'm doing this on a web service if that helps.

I also tried return new ResponseEntity<Integer>(HttpStatus.BAD_REQUEST);, but that doesn't seem to work either.

7
  • What Web-Service Tech do you use? In Jersey I implemented this by deriving my own Exception from WebApplicationException and passed the status code in a constructor. In the web-method you simply throw this custom exception. Commented May 29, 2015 at 14:24
  • I'm using Jave EE, Spring MVC? Commented May 29, 2015 at 14:29
  • Duplicate question I guess: stackoverflow.com/questions/16232833/… Commented May 29, 2015 at 14:31
  • He is returning a String though, I tried Integer, not working :/ Commented May 29, 2015 at 14:36
  • You have to return ResponseEntity<Integer> instead of an int. You have to look at the accepted answer. Commented May 29, 2015 at 14:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.