Including a URL as a parameter in a Java Spring REST web request
I have a REST web service that I would like to take a URL as a parameter
Something along the lines of:
Example call: http://myserver.com/myService/http://www.website.com/files/myfile.docx
The original server mapping was:
@RequestMapping(value="/myService/{url}", method=RequestMethod.GET)
Obviously calling this with any URL that has "/"'s in breaks the mapping to the web service.
I have tried using a regex for a URL in the mapping
@RequestMapping( value="/getBase64/<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>", method=RequestMethod.GET )
but this didn't seem to work either.
Does @RequestMapping support regex like this or should I simp开发者_运维技巧ly replace "\"'s in the parameter with another symbol and then convert back at the server?
http://myserver.com/myService/http://www.website.com/files/myfile.docx
It's not RFC-compliant URL so I'd not expect it to work. You should send an ID of site (that's true restful way). I think that if you want to send URL as string parameter (maybe you can send hashcode instead?) the only way to do it is without http:// scheme and escape slashes properly (not sure if it will work).
The best way however, if you must send and URL in a form that you mentioned (http://website.com/blablabla) is to send POST request and your parameter as a form param (identically as in case of a standard HTTP form).
import javax.ws.rs.POST;
import javax.ws.rs.FormParam;
import javax.ws.rs.Path;
// (...)
@POST
@Path("/myservice/")
public Response myService(@FormParam("url") final String url) {
LOG.debug(String.format("Received URL: %s", url));
}
精彩评论