How to pass String array in #RequestParam in REST GET API call through POSTMAN or DHC REST application? - spring-boot

I have following REST controller in Java Spring Application:
#RequestMapping(
value = "/api/getApplication/{me_userId}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Object getApplication(
#PathVariable String userId,
#RequestParam(value="fieldNames[]", required = false) String[] fieldNames) {
if (fieldNames != null) {
for (String fieldName : fieldNames)
System.out.println(fieldName);
}
...
return null;
}
So I can not succeed in simulating API call from DHC REST of POSTMAN what will pass that fieldNames[].
Does anyone know how to do it?

First of all, your current method does not work because your #PathVariable is wrong. In your #RequestMapping you have the following placeholder in your path: {me_userId}, which means that it will be mapped to a path variable with that name.
However, the only #PathVariable you have is nameless, which means it will use the name of the parameter (userId) in stead.
So before you try to execute your request, you have to change your #RequestMapping into:
#RequestMapping(
value = "/api/getApplication/{userId}", // <-- it's now {userId}
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
Then, if you run the application, you can kinda choose how you want to pass your parameters. Both the following will work:
?fieldNames[]=test,test2
Or:
?fieldNames[]=test&fieldNames[]=test2
Both these results should print the desired results.

Related

Controller Parameter giving a strange error

I have a wrote a project using spring boot my controller were just working fine until I have added
'params' to getmapping annotation
my PlaneTypeVersionedApi Interface down below;
#Validated
#RequestMapping("/version")
public interface PlaneTypeVersionedApi {
#GetMapping(value = "/plane", params = "v1")
ResponseEntity<String> getOnePlaneByProduce1(#RequestParam("plane-type")String planeType);
}
my PlaneTypeVersionedApiImpl Class down below;
#RestController
public class PlaneTypeVersionedApiImpl implements PlaneTypeVersionedApi {
private final PlaneCallerService planeCallerService;
public PlaneTypeVersionedApiImpl (PlaneCallerService planeCallerService) {
this.planeCallerService = planeCallerService;
}
#Override
public ResponseEntity<String> getOnePlaneByProduce1(String planeType) {
return ResponseEntity.ok(planeCallerService.getPlaneType(planeType));
}
}
when I try on Postman for my controller;
localhost:9080/version/plane?plane-type=light-weight?v1
I have 400 bad request and saying
Resolved [org.springframework.web.bind.UnsatisfiedServletRequestParameterException: Parameter conditions "v1" not met for actual request parameters: plane-type={light-weight?v1}]
The syntax of the query parameters on the url is not valid.
Here is the correct syntax : localhost:9080/version/plane?plane-type=light-weight&v1.
https://launchschool.com/books/http/read/what_is_a_url#querystringsparameters
your url is bad not sure what your intent use of params
example 1 valid url = localhost:9080/version/plane?plane-type=light-weight&v1=
using #RequestParam("plane-type") String plane-type
#RequestParam("v1") String v1 is enough to get the value no need use of attribute params
example 2 if you want to use params
#GetMapping(value = "/plane", params = {"v1"}) this mean you want v1 value
ResponseEntity<String> getOnePlaneByProduce1(#RequestParam("v1")String v1);
example 3 you want both
#GetMapping(value = "/plane", params = {"v1","plane-type"}) this mean you want v1 value and plane-type
ResponseEntity<String> getOnePlaneByProduce1(#RequestParam("v1")String v1, #RequestParam("plane-type") String plane-type);
params option just mean that endpoint require to have those params if one you specific is not there it gonna complain

Issue with Spring Rest #RequestMapping when negating params

I have two spring controller methods :
#RequestMapping(value="/requestotp",method = RequestMethod.POST,params = "!applicationId") //new customer
public OTPResponseDTO requestOTP( #RequestBody CustomerDTO customerDTO){
return customerService.requestOTP(customerDTO);
}
#RequestMapping(value="/requestotp",method = RequestMethod.POST,params = {"idNumber","applicationId"}) //existing customer
public String requestOTP( #RequestParam(value="idNumber") String idNumber , #RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
}
using "!applicationId" , I am expecting that when I call the url with applicationId parameter there that the second method will be called , but actually when I pass a request like this :
{"idNumber":"345","applicationId":"64536"}
The first method gets called
This is the part of the params paremeters documentation that I rely on :
Finally, "!myParam" style expressions indicate that the specified
parameter is not supposed to be present in the request.
Can't you just simply delete first request params?
#RequestMapping(value="/requestotp",method = RequestMethod.POST) //new customer
public OTPResponseDTO requestOTP( #RequestBody CustomerDTO customerDTO){
return customerService.requestOTP(customerDTO);
}
The issue actually wasn't with negating the parameter, the issue was that I was sending {"idNumber":"345","applicationId":"64536"} in the POST body and I was expecting the variables to be mapped to the method parameters annotated with #RequestParam ... this is not correct ... #RequestParam only map URL parameters .... so the controller was trying to find the best match so it was using the first method as it contained #RequestBody

Access absolute path of resource when using #RequestMapping in spring boot controller

I am using #RquestMapping for mapping url to controller method.
#RestController
#RequestMapping(path = "/api/tasks")
public class TaskController { ....
and methods inside controller have /{id} with request mapping annotations.
#RequestMapping(
path = "/{taskId},
method = RequestMethod.GET
)
public Map<String, Object> methodOne(...
I want to access http method and absolute path (configured path) for that method inside.
i.e. I want to get /api/tasks/{taskId} as value (not the /api/tasks/1 if api is called for task id 1) and GET as method inside of the methodOne.
I checked the HandlerMapping but it returns actual path of resource called by client. Not the configured path for the method / resource.
Any help or guidance would be highly appreciated. Thank you.
String[] pathReqMappingAnnotationOnControllerClass = this.getClass().getAnnotation(RequestMapping.class).path();
Method method = TaskApiController.class.getMethod("getListOfTasks", HttpServletRequest.class, HttpServletResponse.class);
String[] pathReqMappingAnnotationOnControllerMethod = method.getAnnotation(RequestMapping.class).path();
String wholePath = pathReqMappingAnnotationOnControllerClass[0] + pathReqMappingAnnotationOnControllerMethod[0];
//pathReqMappingAnnotationOnControllerMethod will be empty array if method is not annotated for path
#RequestMapping(path = "/{id}", method = [RequestMethod.DELETE])
public void test(#PathVariable("id") String id, HttpServletRequest request) {
\\ Code Here
}
In the method parameter, id is the pathVariable. And request method is accessible in the request variable (Although I do not know what is the point as you are limiting it to only accept GET requests)
As suggested by #codedetector, best option is if you have request object or you can add one if you dont have it.
#RequestMapping(path = "/{taskId}, method = RequestMethod.GET)
public String methodOne(HttpServletRequest request){
String test = request.getRequestURI();
return test;
}
If you dont have request object in your method, with below code you can get any URL on your system.
import org.springframework.hateoas.mvc.ControllerLinkBuilder
...
ControllerLinkBuilder linkBuilder = ControllerLinkBuilder.linkTo(methodOn(YourController.class).getSomeEntityMethod(parameterId, parameterTwoId))
URI methodUri = linkBuilder.Uri()
String methodUrl = methodUri.getPath()
--------Edit
I am not sure why you need in this format "/api/tasks/{taskId}" as value (not the /api/tasks/1 )but i can think of using a constant use it for your #RequestMapping path parameter and then easily after getting absolute path , replace/append it with that constant.
String pathParam ="/{taskId}"
#RequestMapping(path = "/{id}", method = [RequestMethod.DELETE])
public void test(#PathVariable("id") String id, HttpServletRequest request) {
switch(id){
case 1:
method1();
break;
case 2:
method2();
break
....
....
}
}
private void method1(){};
private void method2(){};
private void method3(){};

Spring REST: DTO as parameter - values are not urldecoded

I have a REST controller with a RequestMapping that looks like this:
#RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(SearchParameters searchParameters) {
// ...
}
and call it like that: /data/search?name=some%20value&....
searchParameters is populated, but the values are not being urldecoded. So instead of setting searchParameter's attribute name to "some value" it is "some%20value". How can I instruct Spring to urldecode these values?
One possible solution is to use a Map and have their names stored statically in a class, like :
#RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(#RequestParam Map<String,String> parameters) {
String name = parameters.get(SearchParameters.NAME);
// ...
}
or use the Map to build the Object SearchParameters:
#RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(#RequestParam Map<String,String> parameters) {
SeachParameters searchParameters = new SearchParameters(parameters);
// ...
}
Okay, the problem was not actually the decoding, but the encoding. It was encoded in a test case, although it wouldn't have needed to be. So the URL looking like /data/search?name=some%20value should really have been /data/search?name=some value with actual spaces.

spring mvc truncating special symbols

We have an application created in spring-boot. We have a rest controller in place as below
#RequestMapping(value = "getDataFromSpaceForTypeForSpaceId/{gridName}/{spaceName}/{dataType}/{spaceId}", method = GET, produces = "application/json")
public DetailedJsonView getDataFromSpaceForTypeForSpaceId(#PathVariable String spaceId,
#PathVariable String dataType,
#PathVariable String gridName,
#PathVariable String spaceName) throws Exception {
Object detailedObject = spaceAccessorService.getDetailedDataFromSpaceForTypeNameWithSpaceId(gridName, spaceName, dataType, spaceId);
String detailedXml = ObjectMarshallarService.marshal(detailedObject);
return new DetailedJsonView(detailedXml);
}
for the urls like this it works fine
http://localhost:8080/rest/query/getDataFromSpaceForTypeForSpaceId/COLO/gcmmprivateSpace/com.ambuj.ValuationSliceRun/InterestRates
for the urls like this the last variable spaceId
http://localhost:8080/rest/query/getDataFromSpaceForTypeForSpaceId/COLO/gcmmprivateSpace/com.ambuj.ValuationSliceRun/InterestRates#MUREX:RATES:EMEA:2015-01-01
Now what we see on the above url is that spaceId comes as InterestRates instead of InterestRates#MUREX:RATES:EMEA:2015-01-01.
I am not sure how i can resolve that

Resources