1. 程式人生 > >java webservice - cxf使用總結 一

java webservice - cxf使用總結 一

lin 文檔 log pack tel pen args maven ext

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使用總結 一