好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot整合cxf发布webservice以及调用的方法

webservice 性能不高,但是现在好多公司还是在用,恰好今天在开发的时候对接项目组需要使用到webservice下面来说下简单的案例应用

首先老规矩:引入jar包

?

1

2

3

4

5

<dependency>

  <groupid>org.apache. cxf </groupid>

  <artifactid>cxf-spring-boot-starter-jaxws</artifactid>

  <version> 3.1 . 11 </version>

</dependency>

新增一个公共的返回实体类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

public class baseresult {

  private string issuccess;

  private string errcode;

  private string message;

 

  public string getissuccess() {

  return issuccess;

  }

 

  public void setissuccess(string issuccess) {

  this .issuccess = issuccess;

  }

 

  public string geterrcode() {

  return errcode;

  }

 

  public void seterrcode(string errcode) {

  this .errcode = errcode;

  }

 

  public string getmessage() {

  return message;

  }

 

  public void setmessage(string message) {

  this .message = message;

  }

 

}

其他类继承即可

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@xmlrootelement (name = "testresult" )

public class testresult extends baseresult implements serializable {

 

  private static final long serialversionuid = -7128575337024823798l;

 

  private list<user> data;

 

  public list<user> getdata() {

  return data;

  }

 

  public void setdata(list<user> data) {

  this .data = data;

  }

}

新增user类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

public class user {

  private string name;

  private int age;

 

  public user(string name, int age) {

  super ();

  this .name = name;

  this .age = age;

  }

  public user() {

  super ();

  }

  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;

  }

}

接下来新增服务接口

?

1

2

3

4

5

6

@webservice

public interface testwservice{

  @webmethod

  @webresult

  testresult list3();

}

实现服务接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@service

@webservice (targetnamespace = "http://ws.**.com/" ,//命名空间,一般是接口的包名倒序)

endpointinterface = "com.**.ws.testwsservice" ) //接口全路径

//**自己改自己的包路径

public class testwsservice impl implements testwsservice {

 

  @override

  public testresult list3() {

  list<user> list = new arraylist<user>();

  list.add( new user( "张三" , 23 ));

  list.add( new user( "李四" , 24 ));

  testresult testresult = new testresult();

  testresult.setissuccess( "y" );

  testresult.setdata(list);

  testresult.setmessage( "操作成功" );

  return testresult;

  }

 

}

新增配置类,发布服务

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

import javax.xml.ws.endpoint;

 

import org.apache.cxf.bus;

import org.apache.cxf.bus.spring.springbus;

import org.apache.cxf.jaxws.endpointimpl;

import org.apache.cxf.transport.servlet.cxfservlet;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.boot.web.servlet.servletregistrationbean;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

 

import com.hikvision.hikserviceassign.ws.monitorsurveyws;

import com.hikvision.hikserviceassign.ws.smartlockserviceorderws;

/**

  * webservice 发布服务类

  * @author xupx

  * @date 2018年8月14日 下午4:25:25

  *

  */

@configuration

public class cxfconfig {

  @autowired

  private testwservice testwservice;

  @suppresswarnings ( "all" )

  @bean

  public servletregistrationbean wsservlet() {

   servletregistrationbean bean = new servletregistrationbean( new cxfservlet(), "/soap/*" );

 

   return bean;

  }

 

  @bean (name = bus.default_bus_id)

  public springbus springbus() {

   return new springbus();

  }

  @bean

  public endpoint testwservice() {

   //会找到o2owebservice的实现类,所以实现类只能有一个

   endpointimpl endpoint = new endpointimpl(springbus(), testwservice);

 

   endpoint.publish( "/testwservice" );

 

   return endpoint;

  }

}

启动项目,然后打开路径:localhost:8080/soap 可以查看多个自己发布的服务,如果要发布多个服务,使用多个bean即可

测试调用1:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import org.apache.cxf.endpoint.client;

import org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.boot.test.context. springboot test;

import org.springframework.test.context.junit4.springrunner;

 

@runwith (springrunner. class )

@springboottest

public class springbootcxfapplicationtests {

 

  @test

  public void contextloads() throws exception {

  jaxwsdynamicclientfactory dcf = jaxwsdynamicclientfactory.newinstance();

  client client = dcf.createclient( "http://127.0.0.1:8080/soap/testwservice?wsdl" );

  object[] objects = client.invoke( "list3" ,param1,param2); //list3方法名 后面是可变参数

  //输出调用结果

  system.out.println(objects[ 0 ].getclass());

  system.out.println(objects[ 0 ].tostring());

  }

}

客户端调用,用soapui生成客户端(具体方法自己百度下,不介绍了)

?

1

2

3

4

testwsimplservice implservice = new testwsimplservice ();

  testservicews ws = implservice.gettestservicewsimplport();

  testresult result = ws.list3();

  system.err.println(result);

增加密码校验,以下基础内容引用 http://www.tuohang.net/article/165939.html ,我补充下包依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

import javax.xml.namespace.qname;

 

import org.apache.cxf.binding.soap.soapmessage;

import org.apache.cxf.headers.header;

import org.apache.cxf.helpers.domutils;

import org.apache.cxf.interceptor.fault;

import org.apache.cxf.phase.abstractphaseinterceptor;

import org.apache.cxf.phase.phase;

import org.w3c.dom.document;

import org.w3c.dom.element;

 

 

public class logininterceptor extends abstractphaseinterceptor<soapmessage> {

  private string username= "root" ;

  private string password= "admin" ;

  public logininterceptor(string username, string password) {

   //设置在发送请求前阶段进行拦截

   super (phase.prepare_send);

   this .username=username;

   this .password=password;

  }

 

  @override

  public void handlemessage(soapmessage soapmessage) throws fault {

   list<header> headers = soapmessage.getheaders();

   document doc = domutils.createdocument();

   element auth = doc.createelementns( "http://cxf.wolfcode.cn/" , "securityheader" );

   element username = doc.createelement( "username" );

   element userpass = doc.createelement( "password" );

 

   username.settextcontent(username);

   userpass.settextcontent(password);

 

   auth.appendchild(username);

   auth.appendchild(userpass);

 

   headers.add( 0 , new header( new qname( "securityheader" ),auth));

  }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

import java.util.list;

import javax.xml.soap.soapexception;

import org.apache.commons.lang3.stringutils;

import org.apache.cxf.binding.soap.soapheader;

import org.apache.cxf.binding.soap.soapmessage;

import org.apache.cxf.interceptor.fault;

import org.apache.cxf.phase.abstractphaseinterceptor;

import org.apache.cxf.phase.phase;

import org.w3c.dom.element;

 

import org.w3c.dom.nodelist;

 

import org.apache.cxf.headers.header;

 

public class authinterceptor extends abstractphaseinterceptor<soapmessage> {

  private static final string username= "root" ;

  private static final string password= "admin" ;

 

  public authinterceptor() {

   //定义在哪个阶段进行拦截

   super (phase.pre_protocol);

  }

 

  @override

  public void handlemessage(soapmessage soapmessage) throws fault {

   list<header> headers = null ;

   string username= null ;

   string password= null ;

   try {

    headers = soapmessage.getheaders();

   } catch (exception e) {

    e.printstacktrace();

   }

 

   if (headers == null ) {

    throw new fault( new illegalargumentexception( "找不到header,无法验证用户信息" ));

   }

   //获取用户名,密码

   for (header header : headers) {

    soapheader soapheader = (soapheader) header;

    element e = (element) soapheader.getobject();

    nodelist usernamenode = e.getelementsbytagname( "username" );

    nodelist pwdnode = e.getelementsbytagname( "password" );

     username=usernamenode.item( 0 ).gettextcontent();

     password=pwdnode.item( 0 ).gettextcontent();

    if ( stringutils.isempty(username)||stringutils.isempty(password)){

     throw new fault( new illegalargumentexception( "用户信息为空" ));

    }

   }

   //校验用户名密码

   if (!(username.equals(username) && password.equals(password))){

    soapexception soapexc = new soapexception( "认证失败" );

    throw new fault(soapexc);

   }

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://my.oschina.net/u/3335797/blog/1927794

查看更多关于springboot整合cxf发布webservice以及调用的方法的详细内容...

  阅读:49次