Java 使用NetBeans生成的WebService类向SOAP消息添加ServiceAuthHeader
我已经得到了一个我需要使用的SOAP Web服务的wsdl。我已经使用wsdl在netbeans中创建了webservice类 SOAP标头需要带有用户名和密码的ServiceAuthHeader NetBeans确实生成了ServiceAuthHeader类,但我不知道如何将其添加到使用生成的类发送的SOAP消息中 我知道如何在较低的级别上完成,即创建SOAPMEssage、添加标头、连接到服务并发送它,但我以前从未使用过jws,因为jws为您提供了最基本的功能,我正在努力找出我在任何文档或教程中添加它的位置 生成的ServiceAuthHeader如下所示:Java 使用NetBeans生成的WebService类向SOAP消息添加ServiceAuthHeader,java,soap,netbeans-7,Java,Soap,Netbeans 7,我已经得到了一个我需要使用的SOAP Web服务的wsdl。我已经使用wsdl在netbeans中创建了webservice类 SOAP标头需要带有用户名和密码的ServiceAuthHeader NetBeans确实生成了ServiceAuthHeader类,但我不知道如何将其添加到使用生成的类发送的SOAP消息中 我知道如何在较低的级别上完成,即创建SOAPMEssage、添加标头、连接到服务并发送它,但我以前从未使用过jws,因为jws为您提供了最基本的功能,我正在努力找出我在任何文档或教
package com.theservice.webservice;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
/**
* <p>Java class for ServiceAuthHeader complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="ServiceAuthHeader">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Username" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* <anyAttribute/>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ServiceAuthHeader", propOrder = {
"username",
"password"
})
public class ServiceAuthHeader {
@XmlElement(name = "Username")
protected String username;
@XmlElement(name = "Password")
protected String password;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
/**
* Gets the value of the username property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUsername() {
return username;
}
/**
* Sets the value of the username property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUsername(String value) {
this.username = value;
}
/**
* Gets the value of the password property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getPassword() {
return password;
}
/**
* Sets the value of the password property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setPassword(String value) {
this.password = value;
}
/**
* Gets a map that contains attributes that aren't bound to any typed property on this class.
*
* <p>
* the map is keyed by the name of the attribute and
* the value is the string value of the attribute.
*
* the map returned by this method is live, and you can add new attribute
* by updating the map directly. Because of this design, there's no setter.
*
*
* @return
* always non-null
*/
public Map<QName, String> getOtherAttributes() {
return otherAttributes;
}
}
我可以解析响应,这当然告诉我需要提供凭据
那么,我如何获得实际SOAP头消息的句柄,以便能够添加ServiceAuthHeader呢?我一直在研究创建的Web服务的方法,并看到您可以获得请求上下文,我也看到了如何向http请求头添加凭据,但我没有;我找不到任何地方可以添加到SOAPMEssage中
任何帮助都将不胜感激。谢谢。我在这里找到了答案
您需要创建一个处理程序,然后告诉您的服务使用它
所以我最初的方法只是添加了几行
package com.la.feed.xml.theservice;
import java.util.*;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.ws.handler.*;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name headerName = soapFactory.createName("ServiceAuthHeader", "", "http://www.interhome.com/webservice");
SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
Name username = soapFactory.createName("Username");
SOAPElement usernameElement = headerElement.addChildElement(username);
usernameElement.addTextNode("GB1009688");
Name password = soapFactory.createName("Password");
SOAPElement passwordElement = headerElement.addChildElement(password);
passwordElement.addTextNode("verbier");
} catch (Exception e) {
}
}
return outboundProperty;
}
@Override
public Set getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
}
package com.la.feed.xml.theservice;
import java.util.*;
import javax.xml.ws.handler.*;
public class HeaderHandlerResolver implements HandlerResolver {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
HeaderHandler hh = new HeaderHandler();
handlerChain.add(hh);
return handlerChain;
}
}
HandlerResolver和Handler看起来像这样
package com.la.feed.xml.theservice;
导入java.util.*;
导入javax.xml.soap.Name;
导入javax.xml.soap.SOAPElement;
导入javax.xml.soap.SOAPEnvelope;
导入javax.xml.soap.SOAPFactory;
导入javax.xml.soap.SOAPHeader;
导入javax.xml.soap.SOAPHeaderElement;
导入javax.xml.ws.handler.*;
导入javax.xml.ws.handler.soap.SOAPHandler;
导入javax.xml.ws.handler.soap.SOAPMessageContext;
公共类HeaderHandler实现SOAPHandler{
@凌驾
公共布尔handleMessage(SOAPMessageContext smc){
Boolean outboundProperty=(Boolean)smc.get(MessageContext.MESSAGE\u OUTBOUND\u属性);
if(outboundProperty.booleanValue()){
试一试{
SOAPEnvelope信封=smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader=envelope.addHeader();
SOAPFactory SOAPFactory=SOAPFactory.newInstance();
Name headerName=soapFactory.createName(“ServiceAuthHeader”,“”,”http://www.interhome.com/webservice");
SOAPHeaderElement-headerElement=header.addHeaderElement(headerName);
Name username=soapFactory.createName(“用户名”);
SOAPElement usernameElement=headerElement.addChildElement(用户名);
usernameElement.addTextNode(“GB1009688”);
Name password=soapFactory.createName(“密码”);
SOAPElement passwordElement=headerElement.addChildElement(密码);
passwordElement.addTextNode(“verbier”);
}捕获(例外e){
}
}
归还境外财产;
}
@凌驾
公共集getHeaders(){
返回null;
}
@凌驾
公共布尔handleFault(SOAPMessageContext上下文){
返回true;
}
@凌驾
公共无效关闭(MessageContext上下文){
}
}
包com.la.feed.xml.theservice;
导入java.util.*;
导入javax.xml.ws.handler.*;
公共类HeaderHandlerResolver实现HandlerResolver{
@凌驾
公共列表getHandlerChain(PortInfo PortInfo){
List handlerChain=new ArrayList();
HeaderHandler hh=新HeaderHandler();
扶手链。添加(hh);
返回手柄链;
}
}
它需要稍加修饰,但它确实起作用。找到了你能找到的答案;是一个部署框架。
private static PriceDetailRetunValue priceDetail(PriceDetailInputValue inputValue) {
com.theservice.webservice.WebService service = new com.theservice.webservice.WebService();
HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
service.setHandlerResolver(handlerResolver);
com.theservice.webservice.WebServiceSoap port = service.getWebServiceSoap12();
return port.priceDetail(inputValue);
}
package com.la.feed.xml.theservice;
import java.util.*;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.ws.handler.*;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name headerName = soapFactory.createName("ServiceAuthHeader", "", "http://www.interhome.com/webservice");
SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
Name username = soapFactory.createName("Username");
SOAPElement usernameElement = headerElement.addChildElement(username);
usernameElement.addTextNode("GB1009688");
Name password = soapFactory.createName("Password");
SOAPElement passwordElement = headerElement.addChildElement(password);
passwordElement.addTextNode("verbier");
} catch (Exception e) {
}
}
return outboundProperty;
}
@Override
public Set getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
}
package com.la.feed.xml.theservice;
import java.util.*;
import javax.xml.ws.handler.*;
public class HeaderHandlerResolver implements HandlerResolver {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
HeaderHandler hh = new HeaderHandler();
handlerChain.add(hh);
return handlerChain;
}
}