I have a REST API like this:
#RequestMapping(value = "/services/produce/{_id}", method = RequestMethod.PATCH,
consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public String patchObject(#RequestBody PatchObjectRequest obj,
#PathVariable("_id") String id) {
// some code
}
My problem is that the id that might be given is in the form:
US%2FCA%2FSF%2FPlastic
Which is a URL encoding of "US/CA/SF/Plastic".
My problem is that when a % character is put into the URL the #RequestMapping does not map it to this method and it will return a 404. Is there a way to accept ids that have % character in them as part of the URL?
You are receiving this error because using it as path variable it is decoded and then server tries to match it to a path that doesn't exits. Similar questions where posted a few years ago: How to match a Spring #RequestMapping having a #pathVariable containing "/"?, urlencoded Forward slash is breaking URL.
A good option for you would be to change _id from #PathVariable to #RequestParam and problem solved, and remove it from path.
Hope you can add regex in the path variable like:
#RequestMapping(value = "/services/produce/{_id:.+}",
method = RequestMethod.PATCH,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
Related
I have the below code as my restful service operation.
#GET
#UnitOfWork
#Timed(name = "get-requests")
#Path("/{referenceId}")
public Response get(#Auth #ApiParam(access = "internal") UserPrincipal user,
#ApiParam(name = "id", value = "reference ID", required = true)
#PathParam("referenceId") String id) {
return Response.ok(id).build();
}
However, I noticed if I pass in m1234;5678, I get only m1234 returned. I tried #Path("/{referenceId:.*}"), but it doesn't work.
I also tried use #Encode at the top of the method to make sure the url is not decoded and then try to replace %3B with ";" in the code. But it seems not working also.
Please note that I cannot use Spring framework. Thanks.
The ; denotes a matrix parameter. Use #MatrixParam to get its value.
See also the answers to this question: URL matrix parameters vs. request parameters
Edit: The key of the matrix parameter would be 5678, the value would be null.
There is a way to get achieve what you want by using PathSegment as the type of the parameter instead of String:
#PathParam("referenceId) PathSegment id
In the body of the method, you can use
String idValue = id.getPath();
to get m1234;5678.
I am writing a feign client to consume an endpoint of a PHP API.
I need to call an endpoint which is like :
www.myapp.com/number.php?number[]=1
My Feign Client looks like this:
#FeignClient(name = "testProxy", url = "${service.url}")
public interface NumberProxy {
#RequestMapping(value = INumber.URL, method = RequestMethod.GET)
public String getEvents(#RequestParam("numbers[]") Integer number);
}
The problems is number[].
If I see the feign log to check the GET URL, this is what I see.
GET [https://www.myapp.com/number.php?number[]={number[]}][1] HTTP/1.1
The number[] is not replaced by the actual value and that is what the API call is failing.
Is there a way to deal with this?
P.S.
I know that the PHP API should not have a query parameter like this, but it is what it is and I can not change that.
And I have also tried with List<Integer> for the number variable, but output is same.
Are we talking about a org.springframework.web.bind.annotation.RequestParam if so it shouldn't be the problem of square brackets. For me it works fine:
#RequestMapping(value = "/api/somepath", method = RequestMethod.POST)
public void uploadData(
#RequestPart("file") MultipartFile fileUpload, HttpServletRequest request,
HttpServletResponse response, #RequestParam(name = "param[]") Integer number) {
LOGGER.trace("paramValue=[{}]", number)
}
logs a value passed through a client
What if the problem is in parameter naming? In the first occurence you write numberS[] but further it named as number[]
You should name your parameter just as numbers without the brackets and change the type to a List:
#RequestMapping(value = INumber.URL, method = RequestMethod.GET)
public String getEvents(#RequestParam("numbers") List<Integer> number);
My controller attaches a question mark at the end of a string. It works great for every types of string.
https://www.mywebsite.com/attachQuestionmark/33 returns 33?
https://www.mywebsite.com/attachQuestionmark/hello returns hello?
However it returns not found error for urls such as https:://www.test.com.
https://www.mywebsite.com/attachQuestionmark/https:://www.test.com returns 404 error.
Is there any way to pass a full url to spring mvc controller?
#RequestMapping(
value = MyUrlBuilder.API_CREATIVE_CREATE + "/attachQuestionmark/{string}",
method = RequestMethod.GET,
produces = MediaType.ALL_VALUE)
#ResponseBody
#PMET
public static String attachQustionmark(#PathVariable("url") String value)
{
return value + "?";
}
Try URL-encoding the path variable, eg:
https://www.mywebsite.com/attachQuestionmark/https%3A%3A%2F%2Fwww.test.com
Because otherwise the / inside the variable will be interpreted as another path
In my test application I have a controller with the following method:
#RequestMapping(value = "/{city}", method = RequestMethod.GET)
public #ResponseBody
MyAnwser getAnwser(#PathVariable String city) {
return new MyAnwser(city);
}
which returns the following
{"result":{"valueA":"valueB"}}
I'm looking for a way to remove the first object sign from the response - "{" to get:
"result":{"valueA":"valueB"}
but I can't figure out a way to do it while using #ResponseBody
If you do that, you wouldn't end up with a valid JSON! In other words, you can't mustn't do that.
As you want to Remove First And Last Character of your Received String Then use
String str="your Received String goes here";
str=str.substring(1, str.length()-1)
I am wondering how spring split each parameters of a http request.
By example i have this method definition :
#RequestMapping(value = "/search.do", method = RequestMethod.GET)
public String searchGet(ModelMap model,
#RequestParam(value = "memberId", required = false) Integer memberId,
#RequestParam(value = "member", required = false) String member) {...}
and i use this url :
/search.do?member=T&O=
i get member = T and not member =T&O=
The request params are limited to only memberId and member.
Can i configure spring for solving this problem ?
Some characters in URLs have a special meaning. If they are supposed to be part of a value they need to be escaped.
If your value is T&O= then it needs to be changed to T%26O%3D
Looking at your controller code, your URL should have been
/search.do?memberId=T&member=
Then request parameter names will get mapped correctly.
If you wish to use same URL as mentioned in your question, change controller code to :
public String searchGet(ModelMap model,
#RequestParam(value = "O", required = false) Integer memberId,
#RequestParam(value = "member", required = false) String member) {...}
& is used to seperate request parameters.
URL contain request param name and value in following format
http://host_port_and_url?name1=value1&name2=value2&so_on
In your case
/search.do?member=T&O=
Name -> Value
member -> T
O -> (No value- Blank)
So you are getting correct values