-6

So my task is to handle request without or with incorrect query parameters.

The correct one that return data GET {url}/v1/transactions/?from=2017-01-01&to=2035-12-01&currency=EUR

The request without query parameters GET {url}/v1/transactions/

The request with incorrect query parameter GET {url}/v1/transactions/?from=2017-01-01&to=2035-12-01&currrrrrrrrrrency=EUR

The first one result in HTTP-200, the last two with HTTP-500 System Error. It should be HTTP-400 or some another 4xx for incorrect query parameters. Should I add exceptionHandler for some kind of exception(as it worked before with incorrect content-type etc)? If yes, what kind of exception is thrown when no parameters or invalid are found? Or if there is no such exception, I believe I need to add some validation in my controller. I think the problem would be solved by adding defaultValues, but what if I don't have default values, what do I do to prevent this issue from happening or how could I handle it in another way than http-500.

E_net4
  • 27,810
  • 13
  • 101
  • 139
developer1
  • 527
  • 4
  • 27
  • 2
    I suggest you start by reading a base tutorial that explains to you how to implement restful endpoints using spring. You are basically using us to write such a tutorial, just for you. And that isn't the point of this community. We help you making individual steps, but this place isn't a replacement for you sitting down and **learning** the technology you intend to use. – GhostCat Aug 14 '19 at 07:23
  • Possible duplicate of [Spring MVC Spring Security and Error Handling](https://stackoverflow.com/questions/18322279/spring-mvc-spring-security-and-error-handling) – Kavitha Karunakaran Aug 14 '19 at 07:23
  • @GhostCat calm down, I just asked if there's anything similar to `HttpRequestMethodNotSupportedException` or `MethodArgumentTypeMismatchException` to handle incorrect query parameters automatically. I thought that the required exception is just not handled so it throws the default http-500 error as it was with incorrect content-type header or something similar. Why all the negativity? Just wanted a piece of advice where should investigate to I solve the issue. GlobaExceptionHandler or controller or any other places. Sorry if the question is inappropriate and it has offended you somehow – developer1 Aug 14 '19 at 08:02

2 Answers2

2

The best way to handle this would be:

@RequestParam(name="currency", defaultValue="EUR") String currency

or

@RequestParam(name="currency", required=false) String currency

In second case your should check the existance of currency in your in your controller.

1

I suggest you to place a skeleton of the API, which will give everyone how you have written the APIs.

you can do following ways to handle.

1) For request without parameters, you make the request parameters as mandatory which will give you 400 error code, Parameter missing, some times it will give you different error code. you can handle it.

Java Spring - how to handle missing required request parameters

you can follow this tutorial. https://www.baeldung.com/spring-request-param

2) For request with incorrect parameters,

There are two ways to handle 1) internal type converters, where you place the Date as request param instead of string

How to accept Date params in a GET request to Spring MVC Controller?

2) write a logic in controller to validate input.