• java webservice


    1.创建maven项目 加入pom依赖

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>apache-cxf</artifactId>
        <version>3.0.2</version>
        <type>pom</type>
    </dependency>

    2.编写实体类,接口类与接口实现

    User.java

    package com.itstudy.domain;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name = "User")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "User")
    public class User {
        @XmlElement(nillable = true)
        private Long id;
    
        @XmlElement(nillable = true)
        private String name;
    
        @XmlElement(nillable = true)
        private int age;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    HelloService.java

    package com.itstudy.service;
    
    import com.itstudy.domain.User;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import java.util.List;
    
    @WebService
    public interface HelloService {
    
        @WebMethod
        public String say(@WebParam(name="name")String name);
    
        @WebMethod
        public String sayHello(@WebParam(name="user") User user);
    
        @WebMethod
        public List<User> getList(Long id);
    }

    HelloServiceImpl.java

    package com.itstudy.service.impl;
    
    import com.itstudy.domain.User;
    import com.itstudy.service.HelloService;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import java.util.ArrayList;
    import java.util.List;
    
    @WebService
    public class HelloServiceImpl implements HelloService {
        @WebMethod
        public String say(@WebParam(name = "name") String name) {
            return name + ",您好!";
        }
    
        @WebMethod
        public String sayHello(@WebParam(name = "user") User user) {
            return user.getName() + ",您好!";
        }
    
        @WebMethod
        public List<User> getList(Long id) {
            List<User> list = new ArrayList<User>();
            User user = new User();
            Long sid = 1L;
            user.setId(sid);
            user.setName("张三" + sid);
            list.add(user);
    
            user = new User();
            sid = 2L;
            user.setId(sid);
            user.setName("张三" + sid);
            list.add(user);
    
            return list;
        }
    }

    3.启动 服务

    package com.itstudy;
    
    import com.itstudy.service.HelloService;
    import com.itstudy.service.impl.HelloServiceImpl;
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    
    public class App {
        public static void main(String[] args) {
            //启动方式1  此方式接口实现必须添加注解 @WebService @WebMethod等
    //        System.out.println( "Hello World!" );
    //        HelloServiceImpl implementor = new HelloServiceImpl();
    //        String address = "http://localhost:8080/ws/" + HelloService.class.getSimpleName();
    //        Endpoint.publish(address, implementor);
    
            //启动方式2 此方式接口实现不需要添加注解
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
            factory.setServiceClass(HelloService.class);
            // 发布接口  
            factory.setAddress("http://localhost:8080/ws/" + HelloService.class.getSimpleName());
            factory.setServiceBean(new HelloServiceImpl());
            factory.create();
          
         //发布多个接口,多定义几个factory即可
    } }

    4.动态调用webservice

    package com.itstudy;
    
    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    
    
    public class HelloWorldClient {
        public static void main(String[] argv) {
    
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client =
                    dcf.createClient("http://localhost:8080/ws/HelloService?wsdl");
    
            //sayHello为接口中定义的方法名称张三为传递的参数返回一个Object数组
            Object[] objects = new Object[0];
            try {
                objects = client.invoke("getList", 1L);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            //输出调用结果
            System.out.println(objects[0]);
    
        }
    }

    总结
    1.两种创建方式
      1.1 JaxWsServerFactoryBean启动(接口实现不需要相关注解)
      1.2 Endpoint启动 (接口实现与需要相关注解)
    2.三种调用方式
      2.1动态代理
      2.2拿到现有接口与类
      2.3生成相关代理类

    参考文档

    cxf 发布多个接口

    https://blog.csdn.net/qq_21399933/article/details/78818240

    cxf 三种发布方式与4种调用方式
    https://blog.csdn.net/u011498933/article/details/75355338

    cxf 客户端调用2
    https://blog.csdn.net/z69183787/article/details/53488887
    cxf 客户端调用3
    https://my.oschina.net/nba/blog/482117

    cxf 安全认证1
    https://blog.csdn.net/rangqiwei/article/details/19282271

    cxf 安全认证2
    https://blog.csdn.net/weixin_41138656/article/details/79393366

    cxf实战 创建webservice 与创建restfulapi
    https://blog.csdn.net/kongxx/article/details/7534035

  • 相关阅读:
    noi.ac #30 思维
    bzoj 2330: [SCOI2011]糖果
    bzoj 2326: [HNOI2011]数学作业
    bzoj 2324: [ZJOI2011]营救皮卡丘
    bzoj 2301: [HAOI2011]Problem b
    bzoj 2286: [Sdoi2011消耗战
    bzoj 2282: [Sdoi2011]消防
    bzoj 2257: [Jsoi2009]瓶子和燃料
    bzoj 2245: [SDOI2011]工作安排
    bzoj 2244: [SDOI2011]拦截导弹
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/9755701.html
Copyright © 2020-2023  润新知