1

I implemented a Rest service that creates an Employee. In the response message I want to dynamically set the HTTP Location header with the newly created Employee resource Uri.

The below code is working fine and I am able to see the value in Location header as expected. However I have the Uri hardcoded in the EmpService and I want it to be dynamic. How do I extract/pass Uri information to the EmpService bean?

Config.xml


 <int-http:inbound-gateway
     request-channel="httpPostChannel"
     reply-channel="responseChannel"
     path="/emp"
     supported-methods="POST"   
     message-converters="converters" 
     request-payload-type="com.samples.jaxb.Employee"/>

<int:service-activator ref="empService" method="post"   
                       input-channel="httpPostChannel"   output-channel="responseChannel"/>

EmpService.java

public Message<Employee> post (Message<Employee> msg) {

    Employee emp = empDao.createEmployee(msg.getPayload());
    return MessageBuilder.withPayload(emp)
                         .setHeader(org.springframework.http.HttpHeaders.LOCATION,  "http://localhost:8080/RestSample/emp/" + emp.getEmpId())
                         .build();
}                          
Cisco Java
  • 233
  • 3
  • 13

1 Answers1

1

Actually even right now your URI is dynamic:

"http://localhost:8080/RestSample/emp/" + emp.getEmpId()

OTOH you always can inject it via setter or @Value property during application start from some external property.

Or you even can do that extracting some property/header from the incoming Message.

However I guess you would like to know the host and port you are ran on.

The host you can know via InetAddress.getLocalHost().

The port you can extract via an appropriate ServletContainer vendor API, e.g. for Tomcat: Get the server port number from tomcat with out a request.

With Spring Boot you can just use @LocalServerPort:

* Annotation at the field or method/constructor parameter level that injects the HTTP
* port that got allocated at runtime. Provides a convenient alternative for
* <code>&#064;Value(&quot;${local.server.port}&quot;)</code>.

Although... I guess this one should be enough for:

.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL,
                        request.getURI().toString())

I mean that your incoming Message after <int-http:inbound-gateway> has header set. In my test case with Spring Boot and random Tomcat port it looks like:

"http_requestUrl" -> "http://localhost:64476/service/?name=foo"
Community
  • 1
  • 1
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • I can get the complete Url from header as you mentioned. I was actually looking for any out-of-box option for getting the Uri till context path. 'http://localhost:8080/RestSample' – Cisco Java Aug 26 '16 at 23:39
  • ??? "till context path" for me equals exactly to `http://localhost:8080`. Just because `/RestSample` is a context. All those parts (scheme, host, port) you can get exactly from the `URI` object. Please, demonstrate in your sample which part you would like to build dynamically. – Artem Bilan Aug 27 '16 at 00:18
  • Ok. What's wrong? Just do that in your code ! I still don't understand what you would like to get from us. You have headers, URI class to parse, UriComponentsBuilder and there are some other UriUtils around in Spring and Commons Http Client. On the other hand we always can do that just with code Java abilities! – Artem Bilan Aug 27 '16 at 02:42
  • Great! Does it mean that we can treat the question as closed? – Artem Bilan Aug 27 '16 at 20:15