I am trying to build a rest service in spring boot to update my database..
#RequestMapping(value = "/setrepacking/{transaction_number}/{image_url}", method = RequestMethod.GET)
public String setRepackingDetails(#PathVariable String transaction_number,
#PathVariable String image_url) {
dao.setRepackingDetails(transaction_number, image_url);
return "Updated repacking details for "+transaction_number;
}
But my image_url is like below: And I want to pass below as part of rest component
http://xxxx.com/api/images/get?format=src&type=png
I am trying something like below:
`www.localhost:8080/setrepacking/3500574684/http://thecatapi.com/api/images`/get?format=src&type=png
It is not accepting...
How do I pass the parameter in my broswer??
Appricate any quick solution....
You have to URL encode your image URL path variable before passing it in the request, encoded URL looks like this:
http%3A%2F%2Fxxxx.com%2Fapi%2Fimages%2Fget%3Fformat%3Dsrc%26type%3Dpng
So you request has to look like this:
http://localhost:8080/setrepacking/3500574684/http%3A%2F%2Fxxxx.com%2Fapi%2Fimages%2Fget%3Fformat%3Dsrc%26type%3Dpng
This way you will get your image URL correctly. Also have a look at URLEncoder and URLDecoder
Related
I have a Spring Rest Service like this:
#RequestMapping(value = "/banks", method = RequestMethod.GET)
public List<String> getBanks( #RequestParam(value="name" , required=true) String name) {
...
}
The name must allows special character like parentheses, but the problem is that when i put parentheses on the param, y receive the ASCCI code like #040# on the "name" parameter.
I thought in use #RequestBody with a wrap filter like a posible solution, but the method must change to POST to support the wrap in the Request Body and the api design going to be bad.
So, someone have a solition for support parentheses on the param of a GET Rest Service?
I want to send a POST request from my service to a SpringBoot #RestController. I have a bunch of string parameters that I am sending, but I also have a FormData parameter which is an image (picture argument). If I do it like this:
public createEvent(name, description, fromDate, toDate, userId, picture){
this.http.post(this.baseUrl + 'create',
{
name: name,
description: description,
fromYear: fromDate['year'],
fromMonth: fromDate['month'],
fromDay: fromDate['day'],
toYear: toDate['year'],
toMonth: toDate['month'],
toDay: toDate['day'],
userId: userId,
picture: picture
}).subscribe();
}
And my Controller method looks like this:
#PostMapping(value = "/create")
public void createEvent(#RequestBody Map map){}
The map looks like this:
and I can't get the file.
I can send the FormData as a single parameter in a post request and receive it as a Multipart file in my controller without any problems, but is it possible to send it in the same request with the other parameters?
Apparently, you can append all of the parameters in the FormData object, and access them through #RequestParam in the controller.
Here is the controller:
Here is the postman:
Via form-data, I can get caseId in my controller.
But raw with header, I can't.
I don't know why... Is there anything wrong with my controller ?
Please help, thanks.
edit 1:
Yeah. Add something more
We know, springMVC will bind data for us, but when we use POST request and put data in body via raw and Content-Type:application/json, spring will still bind data? request.getInputStream() will only call once.
edit 2:
I found a way to get the raw.
get the json string.
edit in 11/29/2017
I found that:
Post with raw, I need to use #RequestBody to recive the value.
Here are the example of how to retrieve data using POSTMAN and bind with SpringMVC
#RequestMapping(value = "/user/", method = RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
List<User> users = userService.findAllUsers();
if (users.isEmpty()) {
return new ResponseEntity(HttpStatus.NO_CONTENT);
// You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
You may refer to this article : Spring Boot Rest API Example
Can u bind request param as below and check :
public Object getTcaseByCaseId(#RequestParam("caseId") String caseId) {
i have such redirect within my javascript:
window.location.href = '/webapp/record.action?date='+varDate+'&id=' +
varId;
When i execute this, i invoke my spring-handler as expected:
public void record(Model model, #RequestParam(value="varDate") String date, #RequestParam(value="varId",) String id){...}
But my second parameter "varId" is everytime null. When i'am looking on my HttpServletRequest i see instead of shown url this url:
/webapp/record.action?varDate=2017-07-01&_=1500358872039)#1495183143
org.eclipse.jetty.server.Request#591eaf27
How this url has been created? Why i lost second parameter "varId" ?
you are passing the parameter as id and in spring controller you are trying to retrieve it as varId.
change you code in controller as below:
public void record(Model model, #RequestParam(value="varDate") String date, #RequestParam(value="id",) String id){
// your custom logic
}
I'm trying to pass in an URL as a string parameter to a WEB API GET method.
The controller:
public class LinksController : ApiController
{
public HttpResponseMessage Get(string targetUrl)
{
//query db with targetURL
}
}
The aim is to query the database to see if the URL is stored. This works fine with simple URLs and URLs whose query string contains a single parameter, like:
http://www.youtube.com/watch?v=nLPE4vhSBx4
The problem I'm encountering is specifically when the query string contains multiple parameters, e.g.
http://www.youtube.com/watch?v=nLPE4vhSBx4&feature=youtube_gdata
When debugging, the value of targetUrl is only ".../watch?v=nLPE4vhSBx4" which means &feature=youtube_gdata is lost.
The GET request looks like this:
http://localhost:58056/api/links?targetUrl=http://www.youtube.com/watch? v=nLPE4vhSBx4&feature=youtube_gdata
I've also tried to add the following route in WebApiConfig.cs:
config.Routes.MapHttpRoute(
name: "Links",
routeTemplate: "api/links/{targetUrl}",
defaults: new { controller = "Links", targetUrl= RouteParameter.Optional }
);
But the GET request then results in 400 Bad Request.
So my question is, can't this be done? I would like the complete URL! Or would I need to change the method header to use the [FromBody] attribute and pass it as a JSON object?
You should URLEncode your target URL parameter so that it doesn't get mistaken for subsequent query string parameter. This means the URL you specified should appear as:
http://localhost:58056/api/links?targetUrl=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnLPE4vhSBx4%26feature%3Dyoutube_gdata
And then inside your Get method, URLDecode the string that is passed as a parameter.
Both methods can be found in System.Web.HttpUtility