java webservice - cxf使用总结 一

Posted liuxm2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java webservice - cxf使用总结 一相关的知识,希望对你有一定的参考价值。

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();


    }
}

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 三种发布方式与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

 
















以上是关于java webservice - cxf使用总结 一的主要内容,如果未能解决你的问题,请参考以下文章

webService总结——使用CXF公布和调用webService(不使用Spring)

WebService学习总结——调用第三方提供的webService服务,使用cxf

WebService 四种发布方式总结

webservice CXF总结

2021-05-14 WebService简介 CXF框架及其两种编程方式 JAVA

2021-05-14 WebService简介 CXF框架及其两种编程方式 JAVA