3

I have two Spring MVC applications interconneted between each others with Spring Integration HTTP.

I have an "application1" and an "application2".

The application1 receive this HTTP GET request:

http://localhost:8080/application1/api/persons/search/findByName?name=Name

The application1 manage the request with this @Controller:

@Controller
public class ApplicationController {

    @RequestMapping(value = "/api/{repository}/search/{methodName}", method = RequestMethod.GET)
    public void search(@PathVariable(value="repository") String repository, 
                       @PathVariable(value="methodName") String methodName, 
                       ModelMap model, 
                       HttpServletRequest request, 
                       HttpServletResponse response) {                     
        // handling the request ...
    }
}

Here is what I can see in the request properties:

getRequestURI=/application1/api/persons/search/findByName
getRequestedSessionId=null
getContextPath=/application1
getPathTranslated=null
getAuthType=null
getMethod=GET
getQueryString=name=Name
getServletPath=/api/persons/search/findByName
getPathInfo=null
getRemoteUser=null

I want to "transfer" this request to the application2 using Spring Integration HTTP with an <int-http:outbound-gateway>.

The message for the channel used by the outbound-gateway is originated in the @Controller in this way:

MessagingChannel messagingChannel = (MessagingChannel)appContext.getBean("requestChannelBean");
String payload = repository+"/search/"+methodName;
Message<String> message = MessageBuilder.withPayload(payload).build();
MessageChannel requestChannel = messagingChannel.getRequestChannel();
MessagingTemplate messagingTemplate = new MessagingTemplate();
Message<?> response = messagingTemplate.sendAndReceive(requestChannel, message);

This is the <int-http:outbound-gateway> configuration:

<int-http:outbound-gateway id="gateway" rest-template="restTemplate"
    url="http://localhost:8080/application2/api/service/{pathToCall}"
    http-method="POST" header-mapper="headerMapper" extract-request-payload="true"
    expected-response-type="java.lang.String">
    <int-http:uri-variable name="pathToCall" expression="payload"/>
</int-http:outbound-gateway>

This gateway produces an HTTP POST request towards the url:

http://localhost:8080/application2/api/service/persons/search/findByName

But in this request I lose the original QueryString received by the application1.

I have tried to add the queryString directly to the payload as follows:

String queryString = "";
if (request.getQueryString()!=null)
    queryString = request.getQueryString();
String payload = repository+"/search/"+methodName+"?"+queryString;

But this doesn't work: the produced url is:

http://localhost:8080/application2/api/service/persons/search/findByName%3Fname=Name

The "?" symbol is replaced by "%3F", so the called method is "/service/persons/search/findByName%3Fname=Name", instead of "/service/persons/search/findByName"

I suppose that it depends on the http-method="POST"; I want to use the POST method in any case, because I want to use this "service" for general requests.

So what I have to do in order to transfer the queryString of the original request to the other side in the simplest way as possible?

Thanks in advance.

Alessandro C
  • 3,310
  • 9
  • 46
  • 82
  • I'd say that the behaviour of application1 is valid - to encode the payload. It should rather be application2's responsibility to decode it properly (change `%3F` back to `?`). Do you have control over application2? – Adam Michalik Jul 29 '16 at 08:37
  • Yes, sure. I have the control of both applications. – Alessandro C Jul 29 '16 at 08:38
  • This might help you then: http://stackoverflow.com/q/6138127/466738 – Adam Michalik Jul 29 '16 at 08:42
  • 1
    You have right Adam: it's the application1 that encodes the URL, and I have understood why. I also have found the way to avoid the URI encoding: it's enough to set the `encode-uri="false"` in the `` configuration (the default is "true"). – Alessandro C Jul 29 '16 at 08:51

1 Answers1

4

I have found the answer.

The encoding of the "?" into "%3F" was made by the <int-http:outbound-gateway> beacuse the encode-uri="false" attribute was missing.

The outbound gateway encodes the URI by default, because the encode-uri attribute is setted to true by default.

Alessandro C
  • 3,310
  • 9
  • 46
  • 82