how to check if a request param/query param is passed in the request in Spring MVC app? - spring

I have two different requests to handle
localhost:8080/myapp/status
localhost:8080/myapp/status?v
Please note that in the second request, just a request param is passed. No value is required to be set for it. That is the requirement.
How will I handle this in my controller?
#RequestMapping(value = "/status", method = RequestMethod.GET)
#ResponseBody
public void status(
#RequestParam(value = "v", required = "false") final String verbose) {
//check if v is in query params
...logic
//else
..logic
}

You could use HttpServlerRequest.getParameterMap() like this:
#RequestMapping(value = "/status", method = RequestMethod.GET)
#ResponseBody
public void status(HttpServletRequest request) {
boolean verbose = request.getParameterMap().containsKey("v");
if (verbose) {
...
} else {
...
}
}

Related

I need to pass two #RequestBody parameters into my REST API POST method. How can I achieve this and can I do it using DefaultHttpClient?

My REST API method is:
#RequestMapping(value = "/api/test/summary", method = RequestMethod.POST)
postSummaryData(#RequestBody String one, #RequestBody String two) { ... }
#RequestMapping(value = "/api/test/summary/{one}/{two}", method = RequestMethod.POST) postSummaryData( #RequestParam String one, #RequestParam String two) {
}
Also you can try below:
#RequestMapping(value = "/api/test/summary", method = RequestMethod.POST) postSummaryData() {
RequestAttributes attr = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes)attr).getRequest();
// you can get param from this request
}

Distinguish different values in #PostMapping, #GetMapping or #RequestMapping

#PostMapping(value = { "/weblogin", "/mobilelogin" })
#ResponseStatus(HttpStatus.OK)
public AccessTokenResponseModel login() { // need to distinguish "/weblogin" or "/mobilelogin" }
In spring boot, How can I distinguish the post request comes from "/weblogin" or "/mobilelogin"
in login() method?
You could use the BEST_MATCHING_PATTERN_ATTRIBUTE attribute.
#PostMapping(value = { "/weblogin", "/mobilelogin" })
#ResponseStatus(HttpStatus.OK)
public AccessTokenResponseModel login(HttpServletRequest httpRequest) {
final String requestMapping = ( String ) httpRequest.getAttribute( HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE );
final boolean isMobileLogin = requestMapping.contains("/mobilelogin");
....
}

#JsonGetter doesn't work

I want to make this method return {"valid":true/false}
#ResponseBody
#RequestMapping(value = "/checkUser", method = RequestMethod.POST)
Boolean checkUsersAvailable(#RequestParam("username") String username) {
return contentService.getUser(username) == null;
}
I add these annotations:
#JsonGetter("valid")
#JsonProperty("valid")
But it's still not working.
You can't return a primitive type and get JSON object as a response.
You can use a wrapper or something like that:
#ResponseBody
#RequestMapping(value = "/checkUser", method = RequestMethod.POST)
Map<String, Boolean> checkUsersAvailable(#RequestParam("username") String username) {
boolean result = contentService.getUser(username) == null;
return Collections.singletonMap("success", result);
}

set #RequestMapping value to return spring controller

I have problem with mapping in spring 3 mvc. General I must "send" value (#RequestMapping(value = "/*") to my return statement. How it resolve? I was thinking about this:
#RequestMapping(value = "/*", method = RequestMethod.GET)
public String homeForm( Model model, HttpServletResponse response) throws IOException {
logger.info("Welcome ");
String url=response.getWriter().toString();
return url;
}
Is it good solutions, maybe someone has any advices?
Thaks
If you want to return the string that comes after the slash, you could do something like this:
#RequestMapping(value = "/{foo}", method = RequestMethod.GET)
public #ResponseBody String homeForm(#PathVariable("foo") String foo) {
return foo;
}

Expecting a request param with a "XOR" validation

I have a simple request call in a spring mvc controller
#ResponseBody
#RequestMapping(value = "/url", method = RequestMethod.GET)
public SomeDTO getSth(#RequestParam("paramA") Integer paramA, #RequestParam("paramB") Integer paramB) {
// ...
}
and I want to have either the paramA or the paramB otherwise a normal http response as it currently happens if I do not provide both parameters.
I know there is a required parameter available, but I do not see a way to connect both. Any idea?
I can't think of a very good solution, but the straightforward one seems to be normal, isn't it?
#ResponseBody
#RequestMapping(value = "/url", method = RequestMethod.GET)
public String getSth(#RequestParam(value = "paramA", required = false) Integer paramA,
#RequestParam(value = "paramB", required = false) Integer paramB) {
if (paramA == null ^ paramB == null) {
return "body";
} else {
throw new BadRequestException();
}
}
#ResponseStatus(value = HttpStatus.NOT_FOUND)
public static class BadRequestException extends RuntimeException {
private static final long serialVersionUID = 1L;
}

Resources