【发布时间】:2017-01-03 14:07:20
【问题描述】:
我实现了一个创建 Employee 的 Rest 服务。在响应消息中,我想使用新创建的 Employee 资源 Uri 动态设置 HTTP Location 标头。
下面的代码运行良好,我可以按预期看到 Location 标头中的值。但是我在 EmpService 中硬编码了 Uri,我希望它是动态的。如何提取/传递 Uri 信息到 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();
}
【问题讨论】: