0

Here is my method code:

@RequestMapping(value="/api/restcall", method=GET)
public response methodcall (@RequestParam (value="testId", required=false) String testId, @RequestParam (value="requestId", required=false) String requestId){
//some code
}

I want to validate the request params. Not the value but the field itself.

API call:

localhost:8080/api/restcall?requestId=abcd&testId=xyz

I want to validate that "requestId" and "testId" are sent correctly if sent. Not the value, but the key itself. NOTE: The requestParams are not mandatory fields.

So if below API call is made:

localhost:8080/api/restcall?request=abcd&test=xyz

I want the code to validate that the requestparams are not correct. I tried the @Validate annotation and @Valid annotation. Both did not work.

When incorrect call is made like above, the code is going through as the fields are not mandatory.

I want to know what params are coming in if testId and requestId are not sent in. If I have this information, I can do the validation.

2
  • I don't see anything to be achieved by doing that. Why do you want to do that? Commented Jun 20, 2018 at 18:54
  • What is valid for you in this case? You can't expect spring framework to handle to you every validation you could think of. Commented Jun 20, 2018 at 18:55

2 Answers 2

1

The validation of REST invocations doesn't work in this way.
This validates the values of the sent parameters, not the names of them.
So as the required attribute is set to false for the parameters, no violation constraint occurs.
The invalid names of the sent parameters are probably ignored by the Jackson processing.
If you want to perform such a validation, you should use a custom validator or a custom validation.

For example :

String errorMsg = ""; 
if (StringsUtil.isEmpty(testId)){ 
    errorMsg+="testId param name missing";
}
if (StringsUtil.isEmpty(requestId)){ 
    errorMsg+="requestId param name missing";
}
if (!errorMsg.equals("")){
   throw new ValidationException(errorMsg);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to validate the names of parameters itself? My requirement is in such a way that the fields are not mandatory, but when something comes in other than fields, I have to fail the test saying that the parameters are incorrect. For this reason, I need the fields.
The most simple : valid it yourself. I edited with a very simple example.
I want to know what params are coming in if testId and requestId are not sent in. If I have this information, I can do the validation. Added this to the main question also.
@krish chaitu But you will not know them as these are ignored by Jackson. I have the impression that you try to solve the wrong problem. What you need to say to the client is that its request is invalid and which fields are missing. The rest doesn't matter.
0

You can get a map with all params fields and values with: @RequestParam Map<String,String> allRequestParams. Then you can use containsKey to check for a field.

1 Comment

This is what I was looking for. Thank you much @Mariano Z Lopez.

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.