Sunday, 15 September 2013

resttemplate - spring mvc rest service redirect / forward / proxy -



resttemplate - spring mvc rest service redirect / forward / proxy -

i have build web application using spring mvc framework publish rest services. example:

@controller @requestmapping("/movie") public class moviecontroller { @requestmapping(value = "/{id}", method = requestmethod.get) public @responsebody film getmovie(@pathvariable string id, @requestbody user) { homecoming dataprovider.getmoviebyid(user,id); } . . .

now need deploy application have next problem: clients not have direct access computer on application resides (there firewall). hence need redirection layer on proxy machine (accessible clients) calls actual rest service.

i tried making new phone call using resttemplate: example:

@controller @requestmapping("/movieproxy") public class movieproxycontroller { private string address= "http://xxx.xxx.xxx.xxx:xx/myapp"; @requestmapping(value = "/{id}", method = requestmethod.get) public @responsebody film getmovie(@pathvariable string id,@requestbody user,final httpservletresponse response,final httpservletrequest request) { httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.application_json); resttemplate resttemplate = new resttemplate(); homecoming resttemplate.exchange( address+ request.getpathinfo(), request.getmethod(), new httpentity<t>(user, headers), movie.class); } . . .

this ok need rewrite each method in controller utilize resttemplate. causes redundant serialization/deserialization on proxy machine.

i tried writing generic function using restemplate, did not work out:

@controller @requestmapping("/movieproxy") public class movieproxycontroller { private string address= "http://xxx.xxx.xxx.xxx:xx/myapp"; @requestmapping(value = "/**") public ? redirect(final httpservletresponse response,final httpservletrequest request) { httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.application_json); resttemplate resttemplate = new resttemplate(); homecoming resttemplate.exchange( address+ request.getpathinfo(), request.getmethod(), ? , ?); } . . .

i not find method of resttemplate works request , response objects.

i tried spring redirect , forward. redirect not alter request's client ip address think useless in case. not forwards url either.

is there more appropriate way accomplish this? in advance.

you can mirror/proxy requests this:

private string server = "localhost"; private int port = 8080; @requestmapping("/**") @responsebody public string mirrorrest(@requestbody string body, httpmethod method, httpservletrequest request, httpservletresponse response) throws urisyntaxexception { uri uri = new uri("http", null, server, port, request.getrequesturi(), request.getquerystring(), null); responseentity<string> responseentity = resttemplate.exchange(uri, method, new httpentity<string>(body), string.class); homecoming responseentity.getbody(); }

this not mirror headers.

spring-mvc resttemplate

No comments:

Post a Comment