JAX-WS로 전송 헤더 보내기
기존 JAX-WS(Java™ API for XML-Based Web Services) 클라이언트를 사용하여 애플리케이션에서 정의된 정보를 웹 서비스 요청과 함께 전송 헤더를 사용하여 전송할 수 있습니다. 또한 JAX-WS 웹 서비스 엔드포인트를 사용하면 애플리케이션에서 정의된 정보를 웹 서비스 응답 메시지와 함께 전송 헤더를 사용하여 전송할 수 있습니다.
시작하기 전에
전송 헤더를 보내기 위해 사용으로 설정할 수 있는 JAX-WS 웹 서비스 클라이언트가 필요합니다.
전송 헤더를 보내는 것은 JAX-WS 웹 서비스 클라이언트에서 지원되며 HTTP 및 JMS 전송에 대해서 지원됩니다. 웹 서비스 클라이언트는 게이트웨이 기능과 같은 중간 계층을 통하지 않고 직접 JAX-WS API를 호출해야 합니다.
이 태스크 정보
JAX-WS 프로그래밍 모델을 사용할 때 클라이언트는 웹 서비스 요청 메시지와 함께 전송 헤더의 값을 보내려면 BindingProvider의 RequestContext 오브젝트에 대한 특성을 설정해야 합니다. 이 특성을 설정한 후에 연관된 특성이 null로 설정되거나 BindingProvider 오브젝트가 버려질 때까지 해당 BindingProvider 오브젝트에 대한 후속 원격 메소드 호출의 모든 요청에서 값이 설정됩니다.
JAX-WS 웹 서비스 클라이언트 애플리케이션의 아웃바운드 요청에서 전송 헤더의 값을 전송하려면 클라이언트 코드를 다음과 같이 수정하십시오.
프로시저
결과
웹 서비스 요청 메시지로 전송 헤더를 보내도록 구성되는 JAX-WS 웹 서비스가 있습니다.
예
public class MyApplicationClass {
// Inject an instance of the service's port-type.
@WebServiceRef(EchoService.class)
private EchoPortType port;
// This method will invoke the web service operation and send transport headers on the request.
public void invokeService() {
// Set up the Map that will contain the request headers.
Map<String, Object> requestHeaders = new HashMap<String, Object>();
requestHeaders.put(“MyHeader1”, “This is a string value”);
requestHeaders.put(“MyHeader2”, new Integer(33));
requestHeaders.put(“MyHeader3”, new Boolean(true));
// Set the Map as a property on the RequestContext.
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(com.ibm.websphere.webservices.Constants.REQUEST_TRANSPORT_PROPERTIES, requestHeaders);
// Invoke the web services operation.
String result = port.echoString(“Hello, world!”);
}
}
JAX-WS 엔드포인트에서 응답 전송 헤더 보내기
시작하기 전에
JAX-WS 엔드포인트에서 응답 전송 헤더를 보내는 것은 JAX-WS 클라이언트에서 요청 전송 헤더를 보내는 것과 비슷합니다. 이는 HTTP 및 JMS 전송에 대해서 지원됩니다.
이 태스크 정보
프로시저
- 전송 헤더가 들어 있는 java.util.Map 오브젝트를 작성하십시오.
- 항목(키 및 값)을 응답 메시지로 전송하려는 각 전송 헤더의 맵 오브젝트에 추가하십시오. 이는 클라이언트에 대한 이전 프로시저와 비슷합니다.
- 엔드포인트의 호출과 연관된 MessageContext(javax.xml.ws.handler.MessageContext의 인스턴스)를 검색하십시오.
- com.ibm.websphere.webservices.Constants.RESPONSE_TRANSPORT_PROPERTIES 특성을 사용하여 MessageContext에서 맵 오브젝트를 설정하십시오.
결과
예
@WebService
public class EchoServiceImpl implements EchoServicePortType {
// Inject an instance of WebServiceContext so we can retrieve
// the MessageContext for each invocation of this endpoint.
@Resource
WebServiceContext ctxt;
/**
* Default constructor.
*/
public EchoServiceImpl() {
....
}
public String echoString(String input) {
String result = “Echo result: “ + input;
// Retrieve the MessageContext from the injected WebServiceContext.
MessageContext mc = ctxt.getMessageContext();
// Send some headers back in the response message.
Map<String, Object> responseHeaders = new HashMap<String, Object>();
responseHeaders.put("MyHeader1", "This is a string response value");
responseHeaders.put("MyHeader2", new Integer(33));
responseHeaders.put("MyHeader3”, new Boolean(false));
// Set the response header Map on the MessageContext.
mc.put(com.ibm.websphere.webservices.Constants.RESPONSE_TRANSPORT_PROPERTIES, responseHeaders);
return result;
}
}