CXF 搭建 webservice 服务

港控/mmm° 2022-05-17 04:53 257阅读 0赞
  1. 基于jdk 搭建 webservice

<1> 定义类型

  1. public class Person {
  2. private Integer id;
  3. private String name;
  4. private Integer age;
  5. public Person() {
  6. }
  7. public Person(Integer id, String name, Integer age) {
  8. this.id = id;
  9. this.name = name;
  10. this.age = age;
  11. }
  12. public Integer getId() {
  13. return id;
  14. }
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public Integer getAge() {
  25. return age;
  26. }
  27. public void setAge(Integer age) {
  28. this.age = age;
  29. }
  30. }

<2> 定义接口

  1. @WebService
  2. @SOAPBinding(style= SOAPBinding.Style.DOCUMENT, use= SOAPBinding.Use.LITERAL, parameterStyle= SOAPBinding.ParameterStyle.BARE)
  3. @BindingType(value = "http://www.w3.org/2003/05/soap/bindings/HTTP/")
  4. public interface PeresionService {
  5. void create(Person person);
  6. Person getById(Integer id);
  7. Person getByName(String name);
  8. }

<3> 定义接口实现

  1. @WebService
  2. @SOAPBinding(style= SOAPBinding.Style.DOCUMENT, use= SOAPBinding.Use.LITERAL, parameterStyle= SOAPBinding.ParameterStyle.BARE)
  3. @BindingType(value = "http://www.w3.org/2003/05/soap/bindings/HTTP/")
  4. public class PersonSeviceImpl implements PeresionService {
  5. @Override
  6. public void create(Person person) {
  7. }
  8. @Override
  9. public Person getById(Integer id) {
  10. return new Person(id, "mall", 3);
  11. }
  12. @Override
  13. public Person getByName(String name) {
  14. return new Person(4, name, 3);
  15. }
  16. }

<4> 服务发布

  1. public class PersonTest {
  2. public static void main(String[] args) {
  3. String address = "http://localhost:8686/ws/person";
  4. Endpoint endpoint = Endpoint.publish(address, new PersonSeviceImpl());
  5. System.out.println("发布 person webservice成功!");
  6. }
  7. }

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

  1. 引入cxf 框架

    1. <dependency>
    2. <groupId>org.apache.cxf</groupId>
    3. <artifactId>cxf-rt-core</artifactId>
    4. <version>2.6.2</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.cxf</groupId>
    8. <artifactId>cxf-rt-frontend-jaxws</artifactId>
    9. <version>2.6.2</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.apache.cxf</groupId>
    13. <artifactId>cxf-rt-transports-http</artifactId>
    14. <version>2.6.2</version>
    15. </dependency>
    16. <dependency>
    17. <groupId>org.apache.cxf</groupId>
    18. <artifactId>cxf-rt-transports-http-jetty</artifactId>
    19. <version>2.6.2</version>
    20. </dependency>

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

  1. // cxf 服务端
  2. EndpointImpl endpointimpl = (EndpointImpl) endpoint;
  3. // 添加请求处理前日志拦截器
  4. List<Interceptor<? extends Message>> inInterceptors = endpointimpl.getInInterceptors();
  5. inInterceptors.add(new LoggingInInterceptor());
  6. // 添加请求处理后日志拦截器
  7. endpointimpl.getOutInterceptors().add(new LoggingOutInterceptor());

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

  1. public class CheckUsernamePasswordInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
  2. public CheckUsernamePasswordInterceptor() {
  3. super(Phase.PRE_PROTOCOL);
  4. }
  5. /* 请求消息
  6. <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://service.dmall.dtdream.com/">
  7. <soap:Header>
  8. <atguigu>
  9. <username>itcast</username>
  10. <password>atguigu</password>
  11. </atguigu>
  12. <soap:Header/>
  13. <soap:Body>
  14. <ser:getById>3</ser:getById>
  15. </soap:Body>
  16. </soap:Envelope>
  17. */
  18. @Override
  19. public void handleMessage(SoapMessage message) throws Fault {
  20. Header header = message.getHeader(new QName("atguigu"));
  21. if (null == header) {
  22. throw new Fault(new RuntimeException("用户验证失败"));
  23. }
  24. Element atguigu = (Element) header.getObject();
  25. String username = atguigu.getElementsByTagName("username").item(0).getTextContent();
  26. String password = atguigu.getElementsByTagName("password").item(0).getTextContent();
  27. if ("itcast".equals(username) && "atguigu".equals(password)) {
  28. System.out.println("用户验证通过!");
  29. return;
  30. }
  31. throw new Fault(new RuntimeException("用户验证失败"));
  32. }

添加至服务前置拦截器:

  1. inInterceptors.add(new CheckUsernamePasswordInterceptor());

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读