springboot

来源:互联网 发布:ubuntu 字体文件夹 编辑:程序博客网 时间:2024/06/05 06:17

梦想总要有的,每份工作都是为了下一个机会做准备....


个人比较喜欢jdk原生的webservice,不要添加依赖的jar;


1.接口层:

import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface TestService {    @WebMethod    public String yuan(String param);}
2.实现层:

import javax.jws.WebService;/** * Created by yuanyirui839 on 2017-09-14. * serviceName 为实例的工厂 * 通过工厂生成一个 MyTestWS 实例 * targetNamespace 是知道我生成的地址 * 实际在使用wsimport 工具生成的代码的时候,会有 Response 的相同的地方,去掉Response */@WebService(targetNamespace="http://localhost/client",name="MyTestWS",serviceName="MyWebService")public class TestServiceImpl implements TestService {    @Override    public String yuan(String param) {        return "param=" + param;    }}


3.发布服务:

import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import javax.xml.ws.Endpoint;@SpringBootApplication//@EnableDiscoveryClientpublic class Application {    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);        //webservice 接口暴露        String address = "http://localhost:8088/MyWebService";        //使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用        Endpoint.publish(address, new TestServiceImpl());        System.out.println("发布webservice成功!");    }}


4.查看wsdl
http://localhost:8088/MyWebService?wsdl

5.反编译wsdl成java代码:wsimport -d bin -s src http://localhost:8088/MyWebService?wsdl

wsimport -d bin -s src http://localhost:8088/MyWebService?wsdl-d 生成的class的路径-s生成的java source的路径后边的是webservice的地址

也可以cd到项目具体的路径下:wsimport  src http://localhost:8088/MyWebService?wsdl 


6.编写调用代码:

public class WebserviceTest {    public static void main(String args[]) {        //创建一个用于产生 MyWebService 实例的工厂,MyWebService 类是 wsimport 工具生成的        MyWebService factory = new MyWebService();        //通过工厂生成一个 MyTestWS 实例        MyTestWS wsImpl = factory.getMyTestWSPort();        //调用WebService的sayHello方法 l        String resResult = wsImpl.yuan("返回回来");        System.out.println("调用WebService的yuan结果是:" + resResult);        System.out.println("---------------------------------------------------");    }

7.异常:存在相同的response;这是自动生成时,默认构建的问题。请将文件修改为:
XmlType 的name不要和类名一样即可

@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "yuanres", propOrder = {    "_return"})public class YuanResponse {    @XmlElement(name = "return")    protected String _return;    /**     * Gets the value of the return property.     *      * @return     *     possible object is     *     {@link String }     *          */    public String getReturn() {        return _return;    }    /**     * Sets the value of the return property.     *      * @param value     *     allowed object is     *     {@link String }     *          */    public void setReturn(String value) {        this._return = value;    }}

以上即可完整实现




原创粉丝点击