-2

Let's say there is a url accepting an integer parameter named "param".

@Path("/foo/") 
public Response getFoo(@QueryParam("param") Integer param) 
{ ...... }

If I make a call like this -> /foo/?param=aa (using string value instead of integer), it throws http status code 500 and not 400 since the input seems wrong.

What's the best way to make sure it throws 400, since it is, in fact, bad request?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Neeraj Gupta
  • 765
  • 1
  • 10
  • 18
  • 1
    Are you sure 500 status return because of invalid integer? 500 status is relating to Server error so I think something wrong here. – LHA Jun 15 '16 at 21:36
  • I tried both the way you had your code, as well as the way user1711399 suggested (since he makes a good point about your url format...), but I was getting 400 type errors, so I would suspect a different error is occurring if you're getting a 500 status... – Tyler Jun 15 '16 at 22:44
  • 1
    What the implementation of jax-rs are you using? RestEasy? – Mikhail Chibel Jun 15 '16 at 22:52
  • Going to need more details -- I tried to reproduce your issue, and got 404 errors where you report 5xx. – VoiceOfUnreason Jun 16 '16 at 03:12
  • Jersey will produce 404, which follows the JAX-RS spec. – Paul Samsotha Jun 16 '16 at 05:11
  • @peeskillet: 404 return if @ PathParam used. For this case, 400 is correct status – LHA Jun 16 '16 at 18:25
  • @Loc according to general perception, yes. But not according to the spec. I am too lazy too look up the issue, but believe me, it's there. You can look in the spec, you will find it there. It state that query param and path param should produce 404, and form param and header param should produce 400. Anything that deals with the URL should result in a 404. Not what you expect in the case of query param, but that's the spec – Paul Samsotha Jun 16 '16 at 19:24
  • @Loc see the [last paragraph of 3.2](https://jsr311.java.net/nonav/releases/1.1/spec/spec.html) – Paul Samsotha Jun 17 '16 at 00:56
  • Neeraj, please update the question to include the information requested in the comments – oberlies Feb 27 '19 at 10:24

1 Answers1

0

Try to change:

@Path("/foo/")

to:

@Path("/foo")

and then call

/foo?param=aa

instead of

/foo/?param=aa

I am not sure if it will reolve the issue or not but I have never seen a query parameter being passed right after "/"