我正在使用SOAP Web服务,我需要将来自应用程序级别(即我的主要客户端类别)的信息传递到客户端处理程序级别以进行进一步评估。我在下面列出了我的客户端和我的客户端处理程序。我想扫描用户的值并存储在Handler中的“discountVal”变量中,以便我可以向SOAPHeader添加文本节点。
客户端:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
/**
*
* @author Jack
*/
public class Client
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
client.ItemPriceServiceService service = new client.ItemPriceServiceService();
service.setHandlerResolver(new ClientHandlerResolver());
client.ItemPriceService port = service.getItemPriceServicePort();
String discount = "25";
Scanner scan = new Scanner(System.in);
String item = scan.next();
while(!item.equals("bye"))
{
double price = getPrice(item);
System.out.println(price);
item = scan.next();
}
if(item.equals("bye"))
{
System.exit(0);
}
}
private static double getPrice(java.lang.String arg0) {
client.ItemPriceServiceService service = new client.ItemPriceServiceService();
client.ItemPriceService port = service.getItemPriceServicePort();
return port.getPrice(arg0);
}
private static class ClientHandlerResolver implements HandlerResolver {
public ClientHandlerResolver() {
}
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> hchain = new ArrayList<Handler>();
hchain.add(new clientHandler());
return hchain;
}
}
}
客户处理程序
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.util.Collections;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
/**
*
* @author Jack
*/
public class clientHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext context)
{
Boolean outgoing = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if(outgoing){
try{
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
if (soapHeader == null){
soapHeader = soapEnv.addHeader();
}
//get discount value FROM CLIENT
String discountVal = "25";
QName qname = new QName("http://testOne", "discount");
SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);
soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
soapHeaderElement.addTextNode(discountVal);
soapMsg.saveChanges();
}catch(SOAPException e){
System.err.println(e);
}
}
//continue other handler chain
return true;
}
public Set<QName> getHeaders() {
return Collections.EMPTY_SET;
}
public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}
public void close(MessageContext context) {
}
}
任何帮助将不胜感激!我觉得使用MessageContext会是一种方法,但我不确定从哪里开始。
答案 0 :(得分:6)
你是对的,使用MessageContext是一种将信息从客户端实现传递给处理程序的方法。这是如何做到的:
1)到客户端类添加
((BindingProvider)port).getRequestContext().put("discountID", discount);
2)然后处理
if(context.containsKey("discountID")){
discountVal = context.get("discountID");
}
就是这样。