How to parse dynamic path parameter in handler request method? - light-4j

novice Q over Handler functionality - how can I parse dynamic parameter present in the path, i.e. /path/{parameter} in handleRequest method? I've generated an application based on OpenAPI spec

String clientId = exchange.getQueryParameters().get("clientId").getFirst();
We basically normalized the path parameter to query parameter in the handler module.

Related

Mapping of missing URI variables to Request Mapping

I've developed a Spring API /getFileData, which accepts three URI parameters viz. businessDate/fileName/recordId. It is possible to have any of them can be passed as null. But I still want my API to be working in this case also. How can I achieve this?
I've tried using #GetMapping("getFileData/{businessDate}/{fileName}/{recordId}", "getFileData/{businessDate}//", "getFileData/{businessDate}/{fileName}/")..so on like this for all possible combinations.
#RequestMapping(value = "/getFileData/{businessDate}/{fileName}/{recordId}", method = RequestMethod.GET)
I want this API to be working for all the combination of URI parameters if something get missed out. for example someone requested,
/getFileData///22 or
/getFileData/22Dec2018/ or
/getFileData//treasure/22
You can do that with a #RequestParam of type java.util.Map.
With your design, you will have various #PathVariable params in the controller method as well as the order of path variables /{var1}/{var2}... constructs the url so I don't think it would be possible to skip a path variable in the url and still call the same controller method.

How to modify HTTP request before sending in JMeter through Beanshell pre processor?

I have test case in my csv file. The request URL has a custom variable.
Sample URL : .../abc/$id
I need to replace this id by the id that we get in response from the previous request. I used json extractor to fetch the id from the response. Now I need to update this id in the next test case request. Fetched the Request URL from jmeter context using below code:
String path = ctx.getCurrentSampler().toString();
path.replaceAll("$id", id);
I am not able to set this updated URL in jmeter context (ctx)
You need to assign new path value to path variable
You need to set sampler path to the new value using sampler.setPath() method
So you need to amend your code like:
String path = ctx.getCurrentSampler().toString();
path = path.replaceAll("$id", id);
sampler.setPath(path);
Demo:
Also consider switching to JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black article for details.
Try to avoid the pre / post processors if possible.
Your requirement is very simple and straight forward.
Directly use this in the path - assuming id is the name of variable which has the value.
/abc/${id}

Get current ApplicationPath value in Jersey resource method

I have a jersey 2.0 application using mcv with freemarker templates. In one template I have a form whose action is to resubmit to same url. Say the form url is:
http://my-domain.com/app-base-path/my-form
So the application annotaion is : #ApplicationPath("app-base-path")
and resource path annotation is #Path("my-form"). Great.
I'm trying to set the form action dynamically to be:
<form name="settings" action="${model.formAction}" method="post">
where action should equal: app-base-path/my-form
I'm trying to set the value in the resource by injecting UriInfo. This is what I'm getting:
formAction = uriInfo.getPath();
//result formAction = "my-form"
How can I retrieve the path including app-base-path?
No javascript please!
You can get the absolute URI with uriInfo.getAbsolutePath(), which will return a URI with the full URI of the corresponding resource method.
http://localhost:8080/app-base-path/myform
A URI can be broken down in distinct parts, which the URI class has specific methods to obtain those parts
scheme authority path
--------------------------------------------------------
http :// localhost:8080 /app-base-path/myform
The URI class has method to obtain all of those.
uri.getScheme()
uri.getAuthority()
uri.getPath()
I'm pretty sure I don't need to tell you which one you want :-)

Passing extra parameter through GetAll method of webapi

How to pass an extra parameter through a Get method of webapi because when i pass
GetALL(int page,int limit,int start) it works fine but when in passed one more parameters that is optional and may be null it throws error.
GetAll(int page,int limit,int start,string ? search)
What is the best way to make it working
In Web API optional parameters are those which can be nulled.
If you have type values like int or DateTime, you need to make them nullableby using the ? syntax.
But when they're classes instead of value type, they are directly convertible to null, so you don't need to, and can not, mark them as nullable. SO, your method signature must be simply this:
GetAll(int page,int limit,int start,string search)
If you wanted page, limit or start to be nullable, you should declare them as int?. So, int he signature above this 3 parameters are compulsory, and the last one optional.
EDIT, for OP comment
When you use the default routing for Web API the only way to choose the right method is by parameter matching, i.e. the parameters in the request must match the parameters in the action including the optional parameters. So, there are two ways to make it work:
post the optional parameters as empty parameters. For your case, provided you're using the query string, include a &search= in the URL
modify the routes, so that the parameters are provided as route parameters, and define the search parameter as optional
You can also completely modify the web API routing by including the action in the route. In that case, you have to specify the action in the URLs to invoke the action, but the method can be chosen by action name (given by method name or Action attribute), and not by parameter matching. In that case you don't need to provide the optional parameters. It will work like MVC routing.

Spring Passing request parameter

I want to pass parameter (a String value) to spring controller from a jsp
What is the best or recommended way to do it.
-I don't want to send parameters in URL - #RequestParam may not be suitable
-Should I hook the parameter to a model object and use #ModelAttribute. What if I want just a string value to be passed.. should I create a object with just a string attribute for this purpose?
-Use HttpSevletRequest
Use #RequestParam - it will work with both GET and POST requests. So if you don't want to send them in the URL, use the POST method to submit your form.

Resources