码迷,mamicode.com
首页 > 编程语言 > 详细

java webservice - cxf使用总结 一

时间:2018-10-08 18:14:09      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:get   stat   point   wss   sim   csdn   loser   weixin   gets   

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使用总结 一

标签:get   stat   point   wss   sim   csdn   loser   weixin   gets   

原文地址:https://www.cnblogs.com/liuxm2017/p/9755701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!