(关注这个问题:从Java Web服务客户端获取原始XML响应)
我有一个SOAP消息处理程序,它能够获取Web服务响应的原始XML.我需要将这个XML放到webservice客户端中,这样我就可以在响应之前对响应执行一些XSL转换.我无法找到一种从捕获传入消息的SOAP处理程序中获取数据的好方法,并使原始XML可用于生成(来自WSDL)Web服务客户端.如果这是可行的任何想法?
我想出了这样的事情:
public class CustomSOAPHandler implements javax.xml.ws.handler.soap.SOAPHandler<javax.xml.ws.handler.soap.SOAPMessageContext>
{
private String myXML;
public String getMyXML()
{
return myXML;
}
...
public boolean handleMessage(SOAPMessageContext context)
{
...
myXML = this.getRawXML(context.getMessage());
}
//elsewhere in the application:
...
myService.doSomething(someRequest);
for (Handler h: ((BindingProvider)myService).getBinding().getHandlerChain())
{
if (h instanceof CustomSOAPHandler )
{
System.out.println("HandlerResult: "+ ((CustomSOAPHandler )h).getMyXML());
}
}
Run Code Online (Sandbox Code Playgroud)
在非常简单的测试中,这似乎有效.但这个解决方案感觉有点像廉价黑客.我不喜欢将原始XML设置为链处理程序的成员,我有一种直觉,这违反了许多其他最佳实践.有没有人有更优雅的方式这样做?
我有一个自定义SOAP消息处理程序,用于传入消息,根据正在调用的操作运行不同的代码.我第一次尝试获取操作名称看起来像这样:
public boolean handleMessage(SOAPMessageContext context)
{
String op = context.get(MessageContext.WSDL_OPERATION);
...
Run Code Online (Sandbox Code Playgroud)
此操作失败,因为该属性MessageContext.WSDL_OPERATION似乎永远不会被设置.然后我尝试使用这个:
public boolean handleMessage(SOAPMessageContext context)
{
Map<?, ?> headers = (Map<?, ?>)context.get(MessageContext.HTTP_REQUEST_HEADERS);
ArrayList<String> SOAPAction = ((ArrayList<String>) headers.get("SOAPAction"));
String opName = SOAPAction.get(0);
//opName will be formatted like "urn#myOperation", so the prefix must be removed
opName = ((opName.replace("\"","").split("#"))[1]);
Run Code Online (Sandbox Code Playgroud)
这有效,但我担心可能会出现标题属性"SOAPAction"未设置(或甚至不存在)的情况,或者没有我期望的值.我也有点担心,因为我不知道这是否是获取操作名称的"官方"方式 - 我通过查看context调试器中的内容来弄清楚它.
在处理传入的SOAP消息时,有没有更好的方法来获取操作名称?
我试图在AxisHandler的"handleMessage"方法中获得"HttpServletRequest".我的AxisHandler实现了"SOAPHandler",如下面的代码所示.
我需要在"InBoundDirection"中获得"HttpServletRequest",但它返回"null".
如何在SOAPHandler的"InBoundDirection"中获得"HttpServletRequest"?
谢谢..
@Override
public boolean handleMessage(SOAPMessageContext soapMessageContext) {
boolean direction = ((Boolean) soapMessageContext.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();
if (direction) {
System.out.println("direction = outbound");
} else {
System.out.println("direction = inbound");
HttpServletRequest servletReq=(HttpServletRequest) soapMessageContext.get(MessageContext.SERVLET_REQUEST);
// BECAUSE servletReq is null the following line returns a "nullPointerException"
System.out.println(servletReq.getRemoteHost());
}
return true;
}
Run Code Online (Sandbox Code Playgroud) 我有一个 SOAPHandler。我需要捕获响应的请求
public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleFault(SOAPMessageContext context) {
writeMessageLogging(context);
return true;
}
public boolean handleMessage(SOAPMessageContext context) {
writeMessageLogging(context);
return true;
}
private void writeMessageLogging(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc
.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (logger.isDebugEnabled()) {
if (outboundProperty.booleanValue()) {
logger.debug("Request message");
} else {
logger.debug("Response message:");
}
}
SOAPMessage message = smc.getMessage();
ByteArrayOutputStream out=null;
try {
if (!outboundProperty.booleanValue()) {
String requestXML="Request of the Response, is possible?";
}
out = new ByteArrayOutputStream();
message.writeTo(out);
String strMsg = …Run Code Online (Sandbox Code Playgroud) 我有一个实现SOAPHandler接口的类.handleMessage定义为:
public boolean handleMessage(SOAPMessageContext context) {
SOAPMessage msg = context.getMessage();
SOAPPart part = msg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
// add namespaces
SOAPElement envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-
// add the header with additional elements
Name qname = envelope.createName("Security", "sse", "http://example.com/security.xsd");
element = envelope.addHeader().addChildElement(qname);
qname = envelope.createName("mustUnderstand");
element.addAttribute(qname, "1");
qname = envelope.createName("UsernameToken", "sse", "http://example.com/user.xsd");
element = envelope.getHeader().addHeaderElement(qname);
element.addTextNode("user1");
qname = envelope.createName("Password");
element = envelope.getHeader().addHeaderElement(qname);
element.addTextNode("1234");
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这会生成以下消息:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" …Run Code Online (Sandbox Code Playgroud) 你能解释一下SOAPHandler接口的getHeaders方法到底应该做什么吗?
http://docs.oracle.com/javaee/5/api/javax/xml/ws/handler/soap/SOAPHandler.html#getHeaders%28%29
我不确定它是否会创建额外的标头,或者它是否应该告诉运行时消息应该具有哪些标头.
我一直在扫描互联网寻找详细信息,但我找不到任何.我认为是如此基本和明显,不需要文档LOL :-)
谢谢
我需要创建一个具有特定名称的 Cookie,以便在我的 Web 服务调用中将其发送到另一个具有基于它的路由逻辑的网络。
当我尝试使用 SOAP 处理程序设置为 HTTP 标头时
headers.put("Cookie", Collections.singletonList(cookiename + "='" + cookieValue + "'"));
Run Code Online (Sandbox Code Playgroud)
它是第一次工作。
对此的响应带有JSESSIONID的Set-Cookie。
我随后的请求持有 JSESSIONID 和它在 cookie 中的值,并忽略设置我的自定义 cookie。
有没有办法确保在我的所有请求中默认设置我的 cookie?
soaphandler ×7
java ×6
soap ×3
web-services ×3
jax-ws ×2
xml ×2
axis ×1
cookies ×1
http-headers ×1
servlets ×1