java的WebService实践(cxf)
生活随笔
收集整理的這篇文章主要介紹了
java的WebService实践(cxf)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java的WebService實踐(cxf)
Java發布WebService,結合Spring,通過cxf的方式
難點:1、引用什么jar包;
?
1、創建接口
源碼如下:
package com.nankang;import javax.jws.WebParam; import javax.jws.WebService;@WebService public interface HelloWorld {String sayHi(@WebParam(name="text") String text); }?
2、實現接口
源碼如下:
package com.nankang;import javax.jws.WebService;@WebService(endpointInterface="com.nankang.HelloWorld",serviceName="HelloWorld") public class HelloWorldImpl implements HelloWorld {public String sayHi(String text) {// TODO Auto-generated method stubreturn "Hello" + text;}}?
3、web.xml的配置
源碼如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/webservice/*</url-pattern></servlet-mapping> </web-app>?
4、添加applicationContext.xml
源碼如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><!-- 掃描spring注解配置 --><context:component-scan base-package="com.nankang.contactqueryservice" /><jaxws:endpoint id="helloWorld" implementor="com.nankang.HelloWorldImpl"address="/helloWorld" /></beans>?
5、訪問
http://localhost:8080/WebServiceTest/webservice/helloWorld?wsdl
6、訪問源碼
package com.nankang;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorldClient {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubJaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();svr.setServiceClass(HelloWorld.class);svr.setAddress("http://localhost:8080/WebServiceTest/webservice/helloWorld");HelloWorld hw = (HelloWorld)svr.create();System.out.println(hw.sayHi("ddddaaaa"));} }7、發布示例:
package com.nankang;import javax.xml.ws.Endpoint;public class WebServiceApp {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("web service start");HelloWorldImpl implementor = new HelloWorldImpl();String address = "http://localhost:8080/helloWorld";Endpoint.publish(address, implementor);System.out.println("web service started");}}?
參考:
http://blog.sina.com.cn/s/blog_a0e7e34c0101959p.html
http://www.cnblogs.com/frankliiu-java/articles/1641949.html
posted on 2014-07-20 01:00?daixinet.com 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/sshoub/p/3855719.html
總結
以上是生活随笔為你收集整理的java的WebService实践(cxf)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #25
- 下一篇: poj1321 DFS