Java 端点类中的Spring WS-SOAP响应头修改
我是SpringWS的新手,正在实现一项服务。 为此,我使用EndPoint类,可以成功地接收请求并发送响应 SOAP请求消息在SOAP头中包含我成功提取的重要信息。一些信息也必须在SOAP响应头中发送,但我无法编辑或修改SOAP响应头 我需要帮助了解创建SOAP响应头和SOAP响应消息的最佳方法和最佳实践 您可以在下面找到我的端点类:Java 端点类中的Spring WS-SOAP响应头修改,java,soap,spring-ws,endpoint,Java,Soap,Spring Ws,Endpoint,我是SpringWS的新手,正在实现一项服务。 为此,我使用EndPoint类,可以成功地接收请求并发送响应 SOAP请求消息在SOAP头中包含我成功提取的重要信息。一些信息也必须在SOAP响应头中发送,但我无法编辑或修改SOAP响应头 我需要帮助了解创建SOAP响应头和SOAP响应消息的最佳方法和最佳实践 您可以在下面找到我的端点类: @Endpoint public class EndpointAccountInformationInquiry { private static
@Endpoint
public class EndpointAccountInformationInquiry {
private static final String TARGET_NAMESPACE = "http://www.sample.com/inquiry/GetAccountInformation";
@Autowired
private ServiceAccountInformation service;
@PayloadRoot(localPart = "GetAccountInformationRq", namespace = TARGET_NAMESPACE)
public @ResponsePayload GetAccountInformationRs handleRequest(@RequestPayload GetAccountInformationRq request, MessageContext messageContext) throws JAXBException, TransformerException {
/*****************************************************************
* Parse the request header and body
* Also create response body and header
*****************************************************************/
SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext.getRequest();
SoapHeader soapRequestHeader = soapRequest.getSoapHeader();
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
SoapHeader soapResponseHeader = soapResponse.getSoapHeader();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
MyHeaderClassRq myHeaderClassRq = null;
JAXBContext jaxbContext = JAXBContext.newInstance(MyHeaderClassRq.class);
Iterator<SoapHeaderElement> itr = soapRequestHeader.examineAllHeaderElements();
while (itr.hasNext()) {
SoapHeaderElement ele = itr.next();
myHeaderClassRq = (MyHeaderClassRq)jaxbContext.createUnmarshaller().unmarshal(ele.getSource());
transformer.transform(ele.getSource(), soapResponseHeader.getResult());
}
/*****************************************************************
* Call the handler function
* This handler function is asynchronous
*****************************************************************/
service.handleRequest(request, msgHdrRq);
/*****************************************************************
* Set the response body and header over here
*****************************************************************/
//TODO: I want to modify my response header over here....
GetAccountInformationRs response = new GetAccountInformationRs();
return response;
}
}
@Endpoint
公共类EndpointAccountInformation查询{
私有静态最终字符串目标_命名空间=”http://www.sample.com/inquiry/GetAccountInformation";
@自动连线
私人服务会计信息服务;
@PayloadRoot(localPart=“GetAccountInformationRq”,命名空间=目标\命名空间)
public@ResponsePayload GetAccountInformationRs HandlerRequest(@RequestPayload GetAccountInformationRq request,MessageContext MessageContext)抛出JAXBEException、TransformerException{
/*****************************************************************
*解析请求头和请求体
*还要创建响应正文和标题
*****************************************************************/
SaajSoapMessage soapRequest=(SaajSoapMessage)messageContext.getRequest();
SoapHeader soapRequestHeader=soapRequest.getSoapHeader();
SaajSoapMessage soapResponse=(SaajSoapMessage)messageContext.getResponse();
SoapHeader soapResponseHeader=soapResponse.getSoapHeader();
TransformerFactory TransformerFactory=TransformerFactory.newInstance();
Transformer Transformer=transformerFactory.newTransformer();
MyHeaderClassRq MyHeaderClassRq=null;
JAXBContext JAXBContext=JAXBContext.newInstance(MyHeaderClassRq.class);
迭代器itr=soapRequestHeader.TesteallHeaderElements();
while(itr.hasNext()){
SoapHeaderElement ele=itr.next();
myHeaderClassRq=(myHeaderClassRq)jaxbContext.createUnmarshaller().unmarshal(ele.getSource());
transform(ele.getSource(),soapResponseHeader.getResult());
}
/*****************************************************************
*调用处理函数
*此处理程序函数是异步的
*****************************************************************/
服务.HandlerRequest(请求,msgHdrRq);
/*****************************************************************
*在这里设置响应主体和标题
*****************************************************************/
//TODO:我想在这里修改我的响应标题。。。。
GetAccountInformationRs响应=新建GetAccountInformationRs();
返回响应;
}
}
我自己解决了
您可以在第节中看到以下代码:
/*****************************************************************
*创建响应主体和标题
*然后送回去
*****************************************************************/
@Endpoint
public class EndpointAccountInformationInquiry {
// private Logger logger = Logger.getLogger(EndpointAccountInformationInquiry.class);
private static final String TARGET_NAMESPACE = "http://www.sample.com/inquiry/GetAccountInformation";
@Autowired
private ServiceAccountInformation service;
@PayloadRoot(localPart = "GetAccountInformationRq", namespace = TARGET_NAMESPACE)
public @ResponsePayload GetAccountInformationRs handleRequest(@RequestPayload GetAccountInformationRq request, MessageContext messageContext) throws JAXBException, TransformerException {
/*****************************************************************
* Parse the request header and body
* Also create response body and header
*****************************************************************/
SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext.getRequest();
SoapHeader soapRequestHeader = soapRequest.getSoapHeader();
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
SoapHeader soapResponseHeader = soapResponse.getSoapHeader();
MyHeaderClassRq myHeaderClassRq = null;
JAXBContext jaxbContext = JAXBContext.newInstance(MyHeaderClassRq.class);
Iterator<SoapHeaderElement> itr = reqheader.examineAllHeaderElements();
while (itr.hasNext()) {
SoapHeaderElement ele = itr.next();
myHeaderClassRq = (MyHeaderClassRq)jaxbContext.createUnmarshaller().unmarshal(ele.getSource());
}
/*****************************************************************
* Call the handler function
* This handler function is asynchronous
*****************************************************************/
service.handleRequest(request, myHeaderClassRq);
/*****************************************************************
* Create response body and header
* And send back
*****************************************************************/
//Response header
MyHeaderClassRs myHeaderClassRs = new MsgHdrRs();
//Set header values here
//Response body
GetAccountInformationRs response = new GetAccountInformationRs();
/*****************************************************************
* Send response back
*****************************************************************/
jaxbContext = JAXBContext.newInstance(MyHeaderClassRs.class);
jaxbContext.createMarshaller().marshal(myHeaderClassRs, soapResponseHeader.getResult());
return response;
}
}
@Endpoint
公共类EndpointAccountInformation查询{
//私有记录器=Logger.getLogger(EndpointAccountInformationInquiry.class);
私有静态最终字符串目标_命名空间=”http://www.sample.com/inquiry/GetAccountInformation";
@自动连线
私人服务会计信息服务;
@PayloadRoot(localPart=“GetAccountInformationRq”,命名空间=目标\命名空间)
public@ResponsePayload GetAccountInformationRs HandlerRequest(@RequestPayload GetAccountInformationRq request,MessageContext MessageContext)抛出JAXBEException、TransformerException{
/*****************************************************************
*解析请求头和请求体
*还要创建响应正文和标题
*****************************************************************/
SaajSoapMessage soapRequest=(SaajSoapMessage)messageContext.getRequest();
SoapHeader soapRequestHeader=soapRequest.getSoapHeader();
SaajSoapMessage soapResponse=(SaajSoapMessage)messageContext.getResponse();
SoapHeader soapResponseHeader=soapResponse.getSoapHeader();
MyHeaderClassRq MyHeaderClassRq=null;
JAXBContext JAXBContext=JAXBContext.newInstance(MyHeaderClassRq.class);
迭代器itr=reqheader.TesteallHeaderElements();
while(itr.hasNext()){
SoapHeaderElement ele=itr.next();
myHeaderClassRq=(myHeaderClassRq)jaxbContext.createUnmarshaller().unmarshal(ele.getSource());
}
/*****************************************************************
*调用处理函数
*此处理程序函数是异步的
*****************************************************************/
服务.HandlerRequest(请求,myHeaderClassRq);
/*****************************************************************
*创建响应主体和标题
*然后送回去
*****************************************************************/
//响应头
MyHeaderClassRs MyHeaderClassRs=新的MsgHdrRs();
//在此处设置标题值
//响应体
GetAccountInformationRs响应=新建GetAccountInformationRs();
/*****************************************************************
*发回响应
*****************************************************************/
jaxbContext=jaxbContext.newInstance(MyHeaderClassRs.class);
jaxbContext.createMarshaller().marshal(myHeaderClassRs,soapResponseHeader.getResult());
返回响应;
}
}