I am using Jersey/java to create a web service that runs on tomca7.
When I pass a URL as a parameter in the #PathParam it does not display anything, but when it is a regular string it works fine.
Here is a modified demo of what I am doing..
For example if I put:
localhost/app/.../broaders/test
it will display: test
But if i put:
localhost/app/.../broaders/http%3A%2F%2Ftematres.befdata.biow.uni-leipzig.de%2Fvocab%2F%3Ftema%3D254
or even just
localhost/app/..../broaders/http%3A2F2F
it does not display anything.
#GET
#Path("broaders/{k}")
#produces(MediaType.APPLICATION_JSON)
#public String getBroader(#PathParam("k") String k){
return k;
}
I added the -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true to Catalina.proprieties but without luck.
Maybe you should pass the url as a parameter. Now, with the allow_encoded_slash you are generating urls that aren't broaders/XXXX but broaders/XXX/YYY/ZZZ so they don't match your regexp.
A #PathParam does not include / by default.
Try a regular expression for the #PathParam.
#Path("broaders/{k:.+}")
Related
I am writing a setter for a domain class. What is being saved is an xml that is a response from a web service. It includes the first and last name of the user but that information needs to be masked. So i am trying to accomplish that using regex.
I wrote the following setter method:
public void setOnlineRetroCreditResponse(String xml) {
xml.replaceAll (/(?<=lastName=)([^\s]+)/){lastName ->
lastName[0].replace ( lastName[1], "X".multiply (lastName[1].size()))
}
onlineRetroCreditResponse = xml
}
I am expecting a sting like this one: "FFPAccountNumber2=12345 lastName=Doe" to be substituted and saved to the databse like this "FFPAccountNumber2=12345 lastName=XXX" but that is not working as expected. I tested my regex using different online like this one https://www.freeformatter.com/java-regex-tester.html and that does not seem to be the issue.
Any ideas would be appreciated.
There are two things: 1) you do not assign the replaced value back to the xml variable and 2) you are replacing the match explicitly while you can just do the necessary modifications to the value you have captured to return it.
Actually, you do not even need to capture the non-whitespace character chunk, you may access the whole match in the replaceAll callback. Also, you can use \S instead of [^\s].
Use
public void setOnlineRetroCreditResponse(String xml) {
onlineRetroCreditResponse = xml.replaceAll(/(?<=lastName=)\S+/){lastName ->
"X".multiply(lastName[0].size())
}
}
I am making a GET request in my java code and the URL that i want to use to perform a GET request is:
http://localhost:1111/sms/v1/222222?startTime=2015-12-29T14%3A00%3A00.000Z&endTime=2015-12-29T15%3A00%3A00.000Z
However it is coming up as:
http://localhost:1111/sms/v1/222222?startTime=2015-12-29T14%253A00%253A00.000Z&endTime=2015-12-29T15%253A00%253A00.000Z
Notice that the following characters are being replaced:
% to %25
How do I make sure my request is sent as I want to specify. My code looks like this:
public static String getRequest(String id, String startTs, String endTs) {
Response response = givenAuthJson("username", "password")
.queryParam("startTime", startTs)
.queryParam("endTime", endTs)
.get(BASE_URL+"/sms/v1/{id}", id);
response
.then()
.spec(responseSpec);
return response.asString();
}
And I call this method as:
.getRequest("222222", "2015-12-29T14%3A00%3A00.000Z","2015-12-29T15%3A00%3A00.000Z");
Can you please give an example or answer using the code given? Thanks.
OK - not sure if I'm allowed to answer my own Q but it is for the benefit of all if someone is doing the same thing as me!
I overcame this problem by adding the following
.urlEncodingEnabled(false)
REFERENCE:
how to handle Special character in query param value in Rest assured
Thanks for all who helped.
I need to provide certain role for accessing a URL in the following format:
/connector/{programId}/order/{anything here}
Where programId is an integer value so I'd tried the following and it doesn't work at all.
.antMatchers('/connector/{programId:\\d+}/order/**').access("hasRole('CONNECTOR')")
But when I used ** instead of the programId part it's working well. But how can I make it work with pathVariable (which is always an integer).
You should use RegexRequestMatcher instead of AntPathRequestMatcher.
.regexMatchers("/connector/(\\d+)/order/.*").access("hasRole('CONNECTOR')")
I am using:
.regexMatchers("/connector/(\\d+)/order/.*").access("hasRole('CONNECTOR')")
But server response 405 error
I'm trying to redirect to a controller method which is taking Long and String as argument by using the reverse controller. I use version 2.4 of the play framework.
I've defined this route in the routes file:
GET /games/play/:id controllers.Games.renderGame(id: Long, feedback: String = "")
To call this route, I'm using in a other method redirect():
return redirect(controllers.routes.Games.renderGame(gameId, "test"));
And here is my renderGame() method:
public Result renderGame(Long id, String feedback) {
//do something
return ok(...);
}
In my opinion this actually should work but play gives me a error:
error: method renderGame in class ReverseGames cannot be applied to
given types;
IntelliJ is trying to do it better:
Error picture
If I define the method just with Long as parameter it's working fine but when a add the String I get the error again.
Any idea what's wrong here?
Related to this Question, it actually should work: Play Framework: Redirect to controller method with arguments
I fixed the problem by myself but it tooks me hours. The problem was that I used a = instead of =?. It has to look like this:
GET /games/play/:id controllers.Games.renderGame(id: Long, feedback: String ?= "")
I think the problem is with the route definition try adding feedback params at the url pattern
GET /games/play/:id/:feedback controllers.Games.renderGame(id: Long, feedback: String = "")
cheers.
How to send/pass values from one servlet(consider it as one project) to another servlet(consider as another project). It's showing number format exception. Is it correct to pass values in sendredirect method or is there any other way
Example:
File: uzkpk2.java
String a1=request.getParameter("a[0]");
aa1=Integer.parseInt(a1);
String a2=request.getParameter("a[1]");
aa2=Integer.parseInt(a2);
String a3=request.getParameter("a[2]");
aa3=Integer.parseInt(a3);
String a4=request.getParameter("a[3]");
aa4=Integer.parseInt(a4);
response.sendRedirect("http://localhost:8080/CSP/czkpk1?y="+y+"&a1="+aa1+"&a2="+aa2+"&a3="+aa3+"&a4="+aa4);
}
catch(Exception e)
{
out.println(e);
}
}
}
File: czkpk1.java
aaa1=Integer.parseInt(request.getParameter("aa1"));
aaa2=Integer.parseInt(request.getParameter("aa2"));
aaa3=Integer.parseInt(request.getParameter("aa3"));
aaa4=Integer.parseInt(request.getParameter("aa4"));
You are using the wrong request parameter to get the value.
aaa1=Integer.parseInt(request.getParameter("aa1"));
aaa2=Integer.parseInt(request.getParameter("aa2"));
aaa3=Integer.parseInt(request.getParameter("aa3"));
aaa4=Integer.parseInt(request.getParameter("aa4"));
Instead of this use
aaa1=Integer.parseInt(request.getParameter("a1"));
aaa2=Integer.parseInt(request.getParameter("a2"));
aaa3=Integer.parseInt(request.getParameter("a3"));
aaa4=Integer.parseInt(request.getParameter("a4"));
since in czkpk1.java you are using the variable names instead of parameters passed in the url present in response.sendRedirect();
And one advice chech for only numeric values before parsing it into string.
The best way to do this is use concept of
Servlet Chaining.
-> Write your value in request context as an attribute using request.setAttribute()
-> After forward the request to second servlet using RequestDispatcher.forward()
-> In second servlet read the value using request.getAttribute()