WebService
JDK方式
服务端:
1.在需要访问的类实现上使用@WebService注解
@WebService
public class WeatherImpl implements IWeather {
public String query(String location) {
System.out.println("输入的地点为" + location);
return "晴天";
}
}
2.在main中使用
public class Main {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/webService",new WeatherImpl());
}
}
3.访问描述 http://localhost:8080/webService?wsdl
服务端描述说明![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b3d318938d7ae49ed09eebcbe84e65b1.png)
生成服务端接口
通过cmd进入到客户端src目录,输入import -s . http://localhost:8080/webService?wsdl
客户端使用服务端接口
public class Main {
public static void main(String[] args) {
//通过wsdl文件中的service节点的name中获得此类
WeatherImplService service = new WeatherImplService();
//getPost中输入的类为wsdl中的portType节点
WeatherImpl weather = service.getPort(WeatherImpl.class);
//通过portType中的operation节点获得
String queryResult = weather.query("福州");
System.out.println(queryResult);
}
}
使用网络wsdl说明书生成代码时报错
解决办法
1.将wsdl文件下载到本地
2.将文件中的<s:element ref="s:schema" />
及<s:any />
改为<s:any minOccurs="2" maxOccurs="2" />
3.将生成代码的地址改为下载的本地文件
注解的使用
//endpointInterface用于指定多个接口时,指定提供给外部的接口
@WebService(endpointInterface = "com.zsj.service.IWeather")
public class WeatherImpl implements IWeather {
public @WebResult(name = "tiangQi") String query(@WebParam( name = "location") String location) {
System.out.println("输入的地点为" + location);
return "晴天";
}
@WebMethod(exclude = true)
public void noUsing(){
System.out.println("不让外部使用");
}
}
apache-cxf的使用
依赖
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.4</version>
</dependency>
</dependencies>
安装并配置解析工具
1.下载地址
2.解压后将其的bin目录配置到环境变量中
服务端使用
public class Main {
public static void main(String[] args) {
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:8888/WebServiceCXF");
factoryBean.setServiceClass(ITestService.class);
factoryBean.setServiceBean(new TestServiceImpl());
factoryBean.create();
System.out.println("服务发布成功");
}
}
客户端使用
1.使用cmd进入到需要生成代码的目录,再使用 wsdl2java -d . http://localhost:8888/WebServiceCXF?wsdl
命令生成
2.
public class Main {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
// 1 设置服务地址,没有跟wsdl
factoryBean.setAddress("http://localhost:8888/WebServiceCXF");
// 2 设置服务的接口
factoryBean.setServiceClass(ITestService.class);
// 3 通过工厂过去对象
ITestService testService = (ITestService) factoryBean.create();
String result = testService.getUserName("zsj");
System.out.println(result);
}
}
spring boot整合cxf
代码部分
服务端
servic
@WebService(name = "TestService", // 暴露服务名称
targetNamespace = "http://www.service.zsj.com") //命名空间,一般是接口的包名倒序
public interface TestService {
@WebMethod
@WebResult(name = "String",targetNamespace = "")
String testGetName();
}
service实现类
@WebService(serviceName = "TestService",//与前面接口一致
targetNamespace = "http://www.service.zsj.com", //与前面接口一致
endpointInterface = "com.zsj.service.TestService") //接口地址
@Component
public class TestServiceImpl implements TestService {
@Override
public String testGetName() {
return "spring的cxf";
}
}
配置类
@Configuration
public class CXFConfig {
@Autowired
private Bus bus;
@Autowired
private TestService testService;
//默认servlet路径/*,如果覆写则按照自己定义的来
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
//终端路径
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, testService);
endpoint.getInInterceptors().add(new AuthInterceptor());//添加校验拦截器
endpoint.publish("/TestService");
return endpoint;
}
}
客户端
@Test
public void contextLoads() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/services/TestService?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
// PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("testGetName", "zsj");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
更多:拦截器等的使用