一.服务端的编写
新建项目:jax-ws-server
1.编写pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>web-service-demo</artifactId>
<groupId>com.meboth.bonc.ljf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.meboth.bonc.ljf.jaxws</groupId>
<artifactId>jax-ws-server</artifactId>
<name>jax-ws-server</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.2</version>
</dependency>
<!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.2</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.编写接口:
package com.meboth.bonc.ljf.jaxws.service;
import javax.jws.WebService;
@WebService
public interface UserService {
//问好
public String sayHelloName(String name);
}
package com.meboth.bonc.ljf.jaxws.service;
public class UserServiceImpl implements UserService {
@Override
public String sayHelloName(String name) {
return name+":nihao ,today is duanwujie!!!!";
}
}
3.配置log4j日志:
log4j.rootLogger=INFO,console,FILE
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=INFO
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.File=./web-service.log
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE.MaxFileSize=30MB
4.启动服务
package com.meboth.bonc.ljf.jaxws;
import com.meboth.bonc.ljf.jaxws.service.UserServiceImpl;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
/**
* Hello world!
*
*/
public class JaxServerApp
{
public static void main( String[] args )
{
// 服务工厂
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 设置服务地址
factory.setAddress("http://localhost:8091/user");
// 设置服务类
factory.setServiceBean(new UserServiceImpl());
//添加日志输入输出拦截器
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
// 发布服务
factory.create();
// 提示
System.out.println("webservice服务端启动成功.......");
}
}
6.查看服务:http://localhost:8091/user?wsdl
二.客户端的编写
新建项目:jax-ws-client
1.pom文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>web-service-demo</artifactId>
<groupId>com.meboth.bonc.ljf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.meboth.bonc.ljf.ws.client</groupId>
<artifactId>jax-ws-client</artifactId>
<name>jax-ws-client</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.2</version>
</dependency>
<!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.2</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.客户端调用接口:
package com.meboth.bonc.ljf.ws.client.service;
import javax.jws.WebService;
@WebService(targetNamespace = "http://service.jaxws.ljf.bonc.meboth.com/")
public interface UserClientService {
// public String sayHey(String name);
public String sayHelloName(String name);
}
3.配置日志文件:
log4j.rootLogger=INFO,console,FILE
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=INFO
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.File=./web-service.log
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE.MaxFileSize=30MB
4.启动类:
package com.meboth.bonc.ljf.ws.client;
import com.meboth.bonc.ljf.ws.client.service.UserClientService;
import com.meboth.bonc.ljf.ws.client.service.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
/**
* Hello world!
*
*/
public class JaxClientApp
{
public static void main( String[] args )
{
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
factoryBean.setAddress("http://localhost:8091/user?wsdl");
factoryBean.setServiceClass(UserClientService.class);
UserClientService ucs=factoryBean.create(UserClientService.class);
String result=ucs.sayHelloName("china,中国:");
System.out.println(""+result);
System.out.println( "ws client 调用成功!!!" );
}
}
5.查看调用结果(注意先启动服务端,确保服务端正常):
ID: 1
Address: http://localhost:8091/user?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[226], content-type=[text/xml; charset=UTF-8], Host=[localhost:8091], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 3.1.2]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloName xmlns:ns2="http://service.jaxws.ljf.bonc.meboth.com/"><arg0>china,中国:</arg0></ns2:sayHelloName></soap:Body></soap:Envelope>
--------------------------------------
2021-03-16 20:31:29[ INFO]-org.apache.cxf.services.UserServiceImplService.UserServiceImplPort.UserService(253)-Outbound Message
---------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloNameResponse xmlns:ns2="http://service.jaxws.ljf.bonc.meboth.com/"><return>china,中国::nihao ,today is duanwujie!!!!</return></ns2:sayHelloNameResponse></soap:Body></soap:Envelope>
--------------------------------------
2021-03-16 20:39:04[ INFO]-org.apache.cxf.services.UserServiceImplService.UserServiceImplPort.UserService(253)-Inbound Message
----------------------------
ID: 2
Address: http://localhost:8091/user?wsdl
Http-Method: GET
Content-Type:
Headers: {Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3], accept-encoding=[gzip, deflate, br], Accept-Language=[zh-CN,zh;q=0.9], connection=[keep-alive], Content-Type=[null], Cookie=[__guid=111872281.1496863850773095700.1598844900205.5002], Host=[localhost:8091], Sec-Fetch-Mode=[navigate], Sec-Fetch-Site=[none], Upgrade-Insecure-Requests=[1], User-Agent=[Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36]}
--------------------------------------
服务端: