• java jax-ws webservice编程,入门教程,包含服务端与客户端,编码


    java jax-ws webservice

    就是服务端程序提供接口,客户端,通过服务端提供的jar包(或者打包的类文件),通过jax-ws直接调用服务端暴露的接口来进行操作.

    服务端

    共三个文件

    接口

    1. package com.jaxwsdemo.serveice;
    2. import javax.jws.WebMethod;
    3. import javax.jws.WebParam;
    4. import javax.jws.WebService;
    5. import javax.jws.soap.SOAPBinding;
    6. import javax.jws.soap.SOAPBinding.Style;
    7. @WebService
    8. @SOAPBinding(style = Style.RPC)
    9. public interface ICalculator {
    10. @WebMethod
    11. float add(@WebParam float a, @WebParam float b);
    12. @WebMethod
    13. float minus(@WebParam float a, @WebParam float b);
    14. @WebMethod
    15. float multiply(@WebParam float a, @WebParam float b);
    16. @WebMethod
    17. float divide(@WebParam float a, @WebParam float b);
    18. }

    实现类

    1. package com.jaxwsdemo.serveice.impl;
    2. import com.jaxwsdemo.serveice.ICalculator;
    3. import javax.jws.WebService;
    4. @WebService(endpointInterface = "com.jaxwsdemo.serveice.ICalculator")
    5. public class ICalculatorimpl implements ICalculator {
    6. @Override
    7. public float add(float a, float b) {
    8. System.out.println("a+b="+(a+b));
    9. return a + b;
    10. }
    11. @Override
    12. public float minus(float a, float b) {
    13. System.out.println("a-b="+(a-b));
    14. return a - b;
    15. }
    16. @Override
    17. public float multiply(float a, float b) {
    18. return a * b;
    19. }
    20. @Override
    21. public float divide(float a, float b) {
    22. return a / b;
    23. }
    24. }

    发布main类

    1. package com.jaxwsdemo.main;
    2. import com.jaxwsdemo.serveice.impl.ICalculatorimpl;
    3. import javax.xml.ws.Endpoint;
    4. public class mainTest {
    5. public static void main(String[] args) {
    6. System.out.println("发布开始:发布地址:http://localhost:9998/hw");
    7. String address="http://localhost:9998/hw";
    8. Endpoint.publish(address, new ICalculatorimpl());
    9. System.out.println("发布完成");
    10. }
    11. }

    运行后会显示发布的地址

    此时我们通过访问:http://localhost:9998/hw?wsdl

    得到wsdl

    1. This XML file does not appear to have any style information associated with it. The document tree is shown below.
    2. <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.serveice.jaxwsdemo.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.serveice.jaxwsdemo.com/" name="ICalculatorimplService">
    3. <import namespace="http://serveice.jaxwsdemo.com/" location="http://localhost:9998/hw?wsdl=1"/>
    4. <binding xmlns:ns1="http://serveice.jaxwsdemo.com/" name="ICalculatorimplPortBinding" type="ns1:ICalculator">
    5. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
    6. <operation name="add">
    7. <soap:operation soapAction=""/>
    8. <input>
    9. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    10. input>
    11. <output>
    12. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    13. output>
    14. operation>
    15. <operation name="divide">
    16. <soap:operation soapAction=""/>
    17. <input>
    18. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    19. input>
    20. <output>
    21. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    22. output>
    23. operation>
    24. <operation name="multiply">
    25. <soap:operation soapAction=""/>
    26. <input>
    27. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    28. input>
    29. <output>
    30. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    31. output>
    32. operation>
    33. <operation name="minus">
    34. <soap:operation soapAction=""/>
    35. <input>
    36. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    37. input>
    38. <output>
    39. <soap:body use="literal" namespace="http://serveice.jaxwsdemo.com/"/>
    40. output>
    41. operation>
    42. binding>
    43. <service name="ICalculatorimplService">
    44. <port name="ICalculatorimplPort" binding="tns:ICalculatorimplPortBinding">
    45. <soap:address location="http://localhost:9998/hw"/>
    46. port>
    47. service>
    48. definitions>

    此时我们服务端就算是完事了,服务端主要是提供加减乘除接口, 通过Endpoint.publish(address, new ICalculatorimpl());发布别的服务就可以调用了.

    在编写服务端之前我们先导出服务端类,共于客户端使用

    这里我们使用使用jdk提供的wsimport命令导出

    命令:wsimport -s e:/wyc/work http://localhost:9998/hw?wsdl

    可以看到导出到了指定位置

    小技巧:如果服务端在对方,如何生成服务端的jar包?

    通过cmd命令: wsimport -keep http://localhost:9998/hw?wsdl 

    可直接生成

    生成路径就是cmd打开的位置!

    我们放到将导出的包放到客户端

    客户端编写

    完整目录

    导出的实现类和接口,与在客户端写的接口结构完全不一样,一定要通过命令导出!!

    导出后的接口结构:

    1. package jaxwsdemo.serveice.impl;
    2. import javax.jws.WebMethod;
    3. import javax.jws.WebParam;
    4. import javax.jws.WebResult;
    5. import javax.jws.WebService;
    6. import javax.jws.soap.SOAPBinding;
    7. import javax.xml.ws.Action;
    8. /**
    9. * This class was generated by the JAX-WS RI.
    10. * JAX-WS RI 2.2.9-b130926.1035
    11. * Generated source version: 2.2
    12. *
    13. */
    14. @WebService(name = "ICalculator", targetNamespace = "http://serveice.jaxwsdemo.com/")
    15. @SOAPBinding(style = SOAPBinding.Style.RPC)
    16. public interface ICalculator {
    17. /**
    18. *
    19. * @param arg1
    20. * @param arg0
    21. * @return
    22. * returns float
    23. */
    24. @WebMethod
    25. @WebResult(partName = "return")
    26. @Action(input = "http://serveice.jaxwsdemo.com/ICalculator/addRequest", output = "http://serveice.jaxwsdemo.com/ICalculator/addResponse")
    27. public float add(
    28. @WebParam(name = "arg0", partName = "arg0")
    29. float arg0,
    30. @WebParam(name = "arg1", partName = "arg1")
    31. float arg1);
    32. /**
    33. *
    34. * @param arg1
    35. * @param arg0
    36. * @return
    37. * returns float
    38. */
    39. @WebMethod
    40. @WebResult(partName = "return")
    41. @Action(input = "http://serveice.jaxwsdemo.com/ICalculator/divideRequest", output = "http://serveice.jaxwsdemo.com/ICalculator/divideResponse")
    42. public float divide(
    43. @WebParam(name = "arg0", partName = "arg0")
    44. float arg0,
    45. @WebParam(name = "arg1", partName = "arg1")
    46. float arg1);
    47. /**
    48. *
    49. * @param arg1
    50. * @param arg0
    51. * @return
    52. * returns float
    53. */
    54. @WebMethod
    55. @WebResult(partName = "return")
    56. @Action(input = "http://serveice.jaxwsdemo.com/ICalculator/multiplyRequest", output = "http://serveice.jaxwsdemo.com/ICalculator/multiplyResponse")
    57. public float multiply(
    58. @WebParam(name = "arg0", partName = "arg0")
    59. float arg0,
    60. @WebParam(name = "arg1", partName = "arg1")
    61. float arg1);
    62. /**
    63. *
    64. * @param arg1
    65. * @param arg0
    66. * @return
    67. * returns float
    68. */
    69. @WebMethod
    70. @WebResult(partName = "return")
    71. @Action(input = "http://serveice.jaxwsdemo.com/ICalculator/minusRequest", output = "http://serveice.jaxwsdemo.com/ICalculator/minusResponse")
    72. public float minus(
    73. @WebParam(name = "arg0", partName = "arg0")
    74. float arg0,
    75. @WebParam(name = "arg1", partName = "arg1")
    76. float arg1);
    77. }

    导出后的实现类结构:

    1. package jaxwsdemo.serveice.impl;
    2. import java.net.MalformedURLException;
    3. import java.net.URL;
    4. import javax.xml.namespace.QName;
    5. import javax.xml.ws.Service;
    6. import javax.xml.ws.WebEndpoint;
    7. import javax.xml.ws.WebServiceClient;
    8. import javax.xml.ws.WebServiceException;
    9. import javax.xml.ws.WebServiceFeature;
    10. /**
    11. * This class was generated by the JAX-WS RI.
    12. * JAX-WS RI 2.2.9-b130926.1035
    13. * Generated source version: 2.2
    14. *
    15. */
    16. @WebServiceClient(name = "ICalculatorimplService", targetNamespace = "http://impl.serveice.jaxwsdemo.com/", wsdlLocation = "http://localhost:9998/hw?wsdl")
    17. public class ICalculatorimplService
    18. extends Service
    19. {
    20. private final static URL ICALCULATORIMPLSERVICE_WSDL_LOCATION;
    21. private final static WebServiceException ICALCULATORIMPLSERVICE_EXCEPTION;
    22. private final static QName ICALCULATORIMPLSERVICE_QNAME = new QName("http://impl.serveice.jaxwsdemo.com/", "ICalculatorimplService");
    23. static {
    24. URL url = null;
    25. WebServiceException e = null;
    26. try {
    27. url = new URL("http://localhost:9998/hw?wsdl");
    28. } catch (MalformedURLException ex) {
    29. e = new WebServiceException(ex);
    30. }
    31. ICALCULATORIMPLSERVICE_WSDL_LOCATION = url;
    32. ICALCULATORIMPLSERVICE_EXCEPTION = e;
    33. }
    34. public ICalculatorimplService() {
    35. super(__getWsdlLocation(), ICALCULATORIMPLSERVICE_QNAME);
    36. }
    37. public ICalculatorimplService(WebServiceFeature... features) {
    38. super(__getWsdlLocation(), ICALCULATORIMPLSERVICE_QNAME, features);
    39. }
    40. public ICalculatorimplService(URL wsdlLocation) {
    41. super(wsdlLocation, ICALCULATORIMPLSERVICE_QNAME);
    42. }
    43. public ICalculatorimplService(URL wsdlLocation, WebServiceFeature... features) {
    44. super(wsdlLocation, ICALCULATORIMPLSERVICE_QNAME, features);
    45. }
    46. public ICalculatorimplService(URL wsdlLocation, QName serviceName) {
    47. super(wsdlLocation, serviceName);
    48. }
    49. public ICalculatorimplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
    50. super(wsdlLocation, serviceName, features);
    51. }
    52. /**
    53. *
    54. * @return
    55. * returns ICalculator
    56. */
    57. @WebEndpoint(name = "ICalculatorimplPort")
    58. public ICalculator getICalculatorimplPort() {
    59. return super.getPort(new QName("http://impl.serveice.jaxwsdemo.com/", "ICalculatorimplPort"), ICalculator.class);
    60. }
    61. /**
    62. *
    63. * @param features
    64. * A list of {@link WebServiceFeature} to configure on the proxy. Supported features not in the features parameter will have their default values.
    65. * @return
    66. * returns ICalculator
    67. */
    68. @WebEndpoint(name = "ICalculatorimplPort")
    69. public ICalculator getICalculatorimplPort(WebServiceFeature... features) {
    70. return super.getPort(new QName("http://impl.serveice.jaxwsdemo.com/", "ICalculatorimplPort"), ICalculator.class, features);
    71. }
    72. private static URL __getWsdlLocation() {
    73. if (ICALCULATORIMPLSERVICE_EXCEPTION!= null) {
    74. throw ICALCULATORIMPLSERVICE_EXCEPTION;
    75. }
    76. return ICALCULATORIMPLSERVICE_WSDL_LOCATION;
    77. }
    78. }

    注意一定要在客户端加入服务端提供的jar包(此处jar包指的是serveice包)

    1. import jaxwsdemo.serveice.impl.ICalculator;
    2. import java.net.URL;
    3. import java.net.MalformedURLException;
    4. import javax.xml.namespace.QName;
    5. import javax.xml.ws.Service;
    6. public class CelintMain {
    7. public static void main(String[] args) throws MalformedURLException {
    8. URL url=new URL("http://localhost:9998/hw?wsdl");
    9. QName qname = new QName("http://impl.serveice.jaxwsdemo.com/", "ICalculatorimplService");
    10. Service service = Service.create(url, qname);
    11. ICalculator cal = service.getPort(new QName("http://impl.serveice.jaxwsdemo.com/", "ICalculatorimplPort"),ICalculator.class);
    12. //cal.add((float)1, (float)2);
    13. float rusult = cal.minus(2, 1);
    14. System.out.println(rusult);
    15. }
    16. }

    运行截图

    服务端也会有相应的输出因为我们计算时打印了计算过程

    服务端此时运行窗口截图

    具体注解可自行百度,此文章已粘贴所有项目代码,粘贴即用!

  • 相关阅读:
    I2C知识大全系列三 —— I2C驱动之单片机中的I2C
    计算机毕业设计 基于微信小程序的“共享书角”图书借还管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
    Docker安装anaconda3镜像
    SpringBoot 统一功能的处理
    java--switch语句
    Web服务(Web Service)
    Java项目:SSM课堂听课记录管理系统
    小红书品牌账号怎么运营,如何传播规划?
    Handler
    【算法】选择排序
  • 原文地址:https://blog.csdn.net/test1372965955/article/details/133063996