The difference between passing query params in a webservice - java

What is the difference between passing query params as /cars/id/toyota vs /cars?id=toyota in a webservice?
Is one REST vs other web service type?

Passing parameter in Rest service in the form of Url or query string, both have different significance.
In simple words
/cars/id/toyota in this toyta is variable and your service expecting something after id/{variablename} otherwise it gives an error (endpoint not found). So in this case your variable became mandatory.
/cars?id=toyota in this case your query string (Id) becomes optional.
So use query string whenever you want to make that variable optional. :)

Nothing, obviously–it all depends on how the consuming service expects to retrieve parameters.

It depends on the receiving party (your web server and the framework you use on that web-service). Both or neither could conceivably be REST web-services, depending on your implementation.
Conceptually, the difference lies in the path from the web-server root (/) and parameters:
/cars/id/toyota is a URL to path /cars/id/toyota without parameters
/cars?id=toyota is a URL to path /cars with parameter named id with value toyota

Related

How to generate "lang" parameter at the end of the url without making a request in each controller in Spring?

I want to add internationalization support to Spring project that I'm working on. It works when I add "lang" parameter at the end of the url like;
localhost:8080/someurl?lang=en
However, I can not generate the parameter at the end of the url, I need to make a parameter request from the controller.
I don't think it is a good idea to request lang parameter in each controller. I believe there is a better way to implement but I don't know what it is.
Do you have any suggestion where should I look for it?
Edit: I have a langKey field for the entity User so, I want to generate lang parameter by using the field langKey of the User.
I realized that applying the lang parameter only once is sufficient for the rest of the session so, there is no need to add lang parameter in each controller. It was enough for me to only add the lang parameter for the redirection after login according to users langKey. Then the rest of the session displayed according to language choice of the user stored in database.
I hope that helps for the others.

explicitly get post parameter or query parameter in controller method

I am using the #RequestParam annotation to instruct Spring MVC to inject a "web request parameter" (as the Javadoc calls them) into a method call in my code:
#RequestMapping(path="/signup-submit", method=RequestMethod.POST)
public String signupSubmit(#RequestParam(value="originURL", required=true) String originURL ...) {
...
I am bothered by the fact that this annotation apparently works both for POST parameters and for URL query parameters. I.e. looking at the above code it is not possible to say whether the originURL is a POST body parameter or is a URL query parameter. Is there an annotation I can use to explicitly get it from either one or the other?
This is why I also place "web request parameter" in quotation marks as I don't think this is a technical term and I guess it is used in the loose sense of "some parameter either passed in the POST method body or as a query parameter in the URL".
Contrary to what cularis said there can be both in the parameter map.
The best way I see is to proxy the parameterMap and for each parameter retrieval check if queryString contains "&?=".
Note that parameterName needs to be URL encoded before this check can be made, as Qerub pointed out.
That saves you the parsing and still gives you only URL parameters.
The way can be getting query string..
public List<Topic> getTopics(HttpServletRequest req) {
String queryString = req.getQueryString();
.....
About "Web-Request-Parameter" (#RequestHeader), you are right as it consists of both params i.e. either are part of query or a part of request body.
Not the exact solution to your problem. But seems like there's an easy hack for it. You can use #RequestBody. This only looks for request body. If param is not found in request body, you can be sure that it is from request param.

Type of variable of JDBC Request in JMeter

I have a problem with my JMeter code, particularly with type of variables that are returned by JDBC Request.
This because then i have to make a json with this variables and therefore is most important the type of them otherwise my rest web service returns error.
I made this code:
The problem is on global_state variable because jdbc request save it as a string but the value is a float in my rest service.
What is the solution?
I also tried to use Beanshell PreProcessor to convert it without any result.
Thanks
I think that it resolved, or won't fail during the http request, with this code:
"grid_voltage": "${grid_voltage_1}"

Creating URI with a list query param for JAX-RS

So JAX-RS can accept a query param as a list. It does this by flattening fields so that
users/query?from=100&to=200&orderBy=age&orderBy=name
becomes
from: 100
to: 200
orderBy: ["age", "name"]
I'm using AngularJS to make my client side application, How do would I construct this URI string without manually building it?
Using Angular, this is pretty simple. After defining your resource, let's call it MyResource you can have it injected in e.g. a controller the usual way. Then just do
MyResource.query({from: 100, to: 200, more: 'params as you like'}, ...);
With optional callbacks for successful or error response. This obviously also works for other methods on the resource.
Although I didn't try it, I would say you could also use an array of values for a param, like this
MyResource.query({orderBy: ['age', 'name']);
Seems to be in Angular since February 2013.

Remove invalid request parameter from URL with servlet filter

I have a web application written in Java which uses Struts 1.0. Sometimes when a URL is fired, I can see that it one of the request parameters does not have any name and value like the following ...
http://www.aaa.com/test.do?a=1&b=2&=&d=4&e=5
As can be seen, there is a '&=' which is essentially a parameter with no name and value. I'd like to remove this part from the URL before sending the request to the server. How can I achieve this? Should I use a filter or is there an easier way?

Categories

Resources