CXF 搭建 webservice 服务

1. 基于jdk 搭建 webservice

<1> 定义类型

public class Person {

    private Integer id;
    private String name;
    private Integer age;

    public Person() {
    }

    public Person(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

<2> 定义接口

@WebService
@SOAPBinding(style= SOAPBinding.Style.DOCUMENT, use= SOAPBinding.Use.LITERAL, parameterStyle= SOAPBinding.ParameterStyle.BARE)
@BindingType(value = "http://www.w3.org/2003/05/soap/bindings/HTTP/")
public interface PeresionService {

    void create(Person person);

    Person getById(Integer id);

    Person getByName(String name);
}

<3> 定义接口实现

@WebService
@SOAPBinding(style= SOAPBinding.Style.DOCUMENT, use= SOAPBinding.Use.LITERAL, parameterStyle= SOAPBinding.ParameterStyle.BARE)
@BindingType(value = "http://www.w3.org/2003/05/soap/bindings/HTTP/")
public class PersonSeviceImpl implements PeresionService {

    @Override
    public void create(Person person) {

    }

    @Override
    public Person getById(Integer id) {
        return new Person(id, "mall", 3);
    }

    @Override
    public Person getByName(String name) {
        return new Person(4, name, 3);
    }
}

<4> 服务发布

public class PersonTest {

    public static void main(String[] args) {

        String address = "http://localhost:8686/ws/person";
        Endpoint endpoint = Endpoint.publish(address, new PersonSeviceImpl());

        System.out.println("发布 person webservice成功!");
    }
}

访问wsdl : http://localhost:8686/ws/person?wsdl

2. 引入cxf 框架

    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-core -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-core</artifactId>
      <version>2.6.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>2.6.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>2.6.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>2.6.2</version>
    </dependency>

<1> 引入日志拦截器,记录服务调用情况,服务调用前,服务调用后

        // cxf 服务端
        EndpointImpl endpointimpl = (EndpointImpl) endpoint;

        // 添加请求处理前日志拦截器
        List<Interceptor<? extends Message>> inInterceptors = endpointimpl.getInInterceptors();
        inInterceptors.add(new LoggingInInterceptor());

        // 添加请求处理后日志拦截器
        endpointimpl.getOutInterceptors().add(new LoggingOutInterceptor());

<2> 自定义拦截器,如对soap信息认证:

public class CheckUsernamePasswordInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    public CheckUsernamePasswordInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }

    /*  请求消息
     <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://service.dmall.dtdream.com/">
       <soap:Header>
        <atguigu>
                <username>itcast</username>
                <password>atguigu</password>
          </atguigu>
       <soap:Header/>
       <soap:Body>
          <ser:getById>3</ser:getById>
       </soap:Body>
    </soap:Envelope>

    */

    @Override
    public void handleMessage(SoapMessage message) throws Fault {

        Header header = message.getHeader(new QName("atguigu"));

        if (null == header) {
            throw new Fault(new RuntimeException("用户验证失败"));
        }

        Element atguigu = (Element) header.getObject();

        String username = atguigu.getElementsByTagName("username").item(0).getTextContent();
        String password = atguigu.getElementsByTagName("password").item(0).getTextContent();

        if ("itcast".equals(username) && "atguigu".equals(password)) {
            System.out.println("用户验证通过!");
            return;
        }

        throw new Fault(new RuntimeException("用户验证失败"));
    }

添加至服务前置拦截器:

inInterceptors.add(new CheckUsernamePasswordInterceptor());

猜你喜欢

转载自blog.csdn.net/wjb214149306/article/details/81463008