WebService

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

服务端描述说明在这里插入图片描述
生成服务端接口

通过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();
        }
    }

更多:拦截器等的使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值