由于客户的需求需要开发一个webservice接口,当时在网上看了挺多的但是大部分是客户端的,现在记录一下。说明一下:我整合的是cxf
首先需要导如的依赖
<!-- webservice cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
导入依赖过后你是需要进行一些配置的
/**
* cxf配置类
* @author oKong
*
*/
@Configuration
public class CxfWebServiceConfig {
@Autowired
private Bus bus;
@Autowired
private ExampleWebService exampleWebService;//这是你webservice接口的接口类
@Bean("cxfServletRegistration")
public ServletRegistrationBean dispatcherServlet() {
//注册servlet 拦截/com 开头的请求 不设置 默认为:/services/*
//比如你的发布地址是/ExampleWebService那么你就可以这么请求 http://localhost:8081/cn/ExampleWebService?wsdl
return new ServletRegistrationBean(new CXFServlet(), "/cn/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus SpringBus(){
return new SpringBus();
}
/*
* 发布endpoint
*/
@Bean
public Endpoint endpoint() {
// EndpointImpl endpoint = new EndpointImpl(bus, exampleWebService);
EndpointImpl endpoint = new EndpointImpl(SpringBus(), exampleWebService);
endpoint.publish("/ExampleWebService");//发布地址
return endpoint;
}
}
接口类
@WebService(targetNamespace = "这个是配置命名空间的")
public interface ExampleWebService {
@WebMethod()
public String CommonMethod(@WebParam(name = "request") String request);
}
实现类
//serviceName :服务名称,targetNamespace :命名空间,endpointInterface :端口
@WebService(serviceName = "ExampleWebService",targetNamespace ="http://huangpu.customs.gov.cn"//命名空间一般遵循与包名相反的命名,如果需要在后面加上/xxx只需要在接口类的注解中一起配置,就是两边命名空间度加上/xxx
,endpointInterface = "cn.gov.customs.huangpu.ExampleWebService")
@Configuration
public class ExampleController implements ExampleWebService {
private static final Logger logger= LoggerFactory.getLogger(ExampleController.class);
@Autowired
IExampleService exampleService;
@Override
public String CommonMethod(String request) {
System.out.println(request);
return "成功";
}
//测试
public static void main(String[] args) {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:9096/com/ExampleWebService?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("CommonMethod", "maple");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
如果你收到返回值就说明成功了
你在网页中输入你配置的地址出现下图叶说明配置成功
xml 的发送方式
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mo="http://huangpu.customs.gov.cn" --命名空间>
<soapenv:Header/>
<soapenv:Body>
<mo:CommonMethod >//CommonMethod 是你的方法名,request为你的参数名,
<request><![CDATA[这里是xml的内容]]></request>
</mo:CommonMethod>
</soapenv:Body>
</soapenv:Envelope>
附上xml的工具类
public class XmlUttil {
private static final String ENCODING = "UTF-8";
/**
* XML字符串转JSON对象
*
* @param xml xml字符串
* @return JSON对象
* @throws DocumentException
*/
public static JSONObject xmlToJson(String xml) throws DocumentException {
JSONObject json = new JSONObject();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(xml));
Element root = document.getRootElement();
String qname = root.getQName().getName();
json.put("name",qname);
json.put(root.getName(), elementToJson(root));
return json;
}
/**
* Element对象转JSON对象
*
* @param element Element对象
* @return JSON对象
*/
public static JSONObject elementToJson(Element element) {
JSONObject json = new JSONObject();
for (Object child : element.elements()) {
Element e = (Element) child;
if (e.elements().isEmpty()) {
json.put(e.getName(), e.getText());
} else {
json.put(e.getName(), elementToJson(e));
}
}
return json;
}
/**
* JSON对象转xml字符串
*
* @param json JSON对象
* @return xml字符串
* @throws SAXException
*/
public static String JsonToXml(JSONObject json) throws SAXException {
return jsonToDocument(json).asXML();
}
/**
* JSON对象转Document对象
*
* @param json JSON对象
* @return Document对象
* @throws SAXException
*/
public static Document jsonToDocument(JSONObject json) throws SAXException {
Document document = DocumentHelper.createDocument();
document.setXMLEncoding(ENCODING);
// root对象只能有一个
for (String rootKey : json.keySet()) {
Element root = jsonToElement(json.getJSONObject(rootKey), rootKey);
document.add(root);
break;
}
return document;
}
/**
* JSON对象转Element对象
*
* @param json JSON对象
* @param nodeName 节点名称
* @return Element对象
*/
public static Element jsonToElement(JSONObject json, String nodeName) {
Element node = DocumentHelper.createElement(nodeName);
for (String key : json.keySet()) {
Object child = json.get(key);
if (child instanceof JSONObject) {
node.add(jsonToElement(json.getJSONObject(key), key));
}
else {
Element element = DocumentHelper.createElement(key);
element.setText(json.getString(key));
node.add(element);
}
}
return node;
}
}