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; }}
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成功!"); }}
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; }}
以上即可完整实现
阅读全文
0 0
- springboot
- SpringBoot
- SpringBoot
- springboot
- SpringBoot
- springboot
- SpringBoot
- springboot
- Springboot
- springboot
- SpringBoot
- SpringBoot
- Springboot
- springboot
- springboot
- Springboot
- springboot
- springBoot
- jvm 系统调优,确定高CPU程序代码
- Android4.4修改状态栏颜色
- java读取excel指定的位置
- vsftpd不能显示文件目录的解决方法
- Jenkins远程部署 gitLab配置
- springboot
- BaseActivity封装
- SSH综合项目实战(快递) -- day11 shiro权限控制、后台用户登录
- TRS Database admin数据字段类型总结
- h5 input type=date 怎么进行赋值(回显)
- TypeScript入门知识三(面向对象特性)
- 从C语言不定参数列表到函数调用的入栈方式
- Android6.0系统动态获取权限<一>
- [MTK] Bluetooth 车载系统 > 获取手机电量 SIM卡信号 运营商等 Android6.0