1.pom.xml依赖 propertiescxf.version2.2.7/cxf.version/properties!-- cxf --dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion${cxf.version}/version/dependencydependencygroupIdorg.apache.cxf/groupIdartif
1.pom.xml依赖
<properties> <cxf.version>2.2.7</cxf.version> </properties> <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency>
2. 编写服务端服务接口
@WebService public interface UserWs { public String getName(@WebParam(name = "msg") String msg); } @Slf4j @WebService(endpointInterface = "com.ecs.ws.UserWs", serviceName = "userWs") public class UserWsImpl implements UserWs { public String getName(@WebParam(name="msg") String msg) { log.info(msg + "你好 !"); return msg + "你好 !"; } }
import javax.xml.ws.Endpoint; public class PublishTest { public static void main(String[] args) { UserWsImpl userWs = new UserWsImpl(); String address = "http://localhost:8080/userWs"; Endpoint.publish(address, userWs); System.out.println("service started"); } }
4. 调用服务接口
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class InvokeTest { public static void main(String[] args) { JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); jaxWsProxyFactoryBean.setServiceClass(UserWs.class); jaxWsProxyFactoryBean.setAddress("http://localhost:8080/userWs"); UserWs userWs = (UserWs)jaxWsProxyFactoryBean.create(); String msg = userWs.getName("zhangsan"); System.out.println(msg); } }