加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

用axis实现webService--服务端+客户端

发布时间:2020-12-17 01:24:06 所属栏目:安全 来源:网络整理
导读:一。程序结构 ? 二。所需要的包: 三。编辑web.xml ?xml version="1.0" encoding="utf-8"? !DOCTYPE web-app ??? PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" ??? " http://java.sun.com/dtd/web-app_2_3.dtd" web-app ??? context-par

一。程序结构

?

二。所需要的包:

三。编辑web.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app
??? PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN"
??? " http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
??? <context-param>
?????? <param-name>contextConfigLocation</param-name>
???????<!--tomcat启动时要读的文件-->
?????? <param-value>WEB-INF/beans.xml</param-value>
??? </context-param>
??? <listener>
?????? <listener-class>
?????????? org.springframework.web.context.ContextLoaderListener
?????? </listener-class>
??? </listener>
??? <servlet>
?????? <servlet-name>CXFServlet</servlet-name>
?????? <display-name>CXF Servlet</display-name>
?????? <servlet-class>
?????????? org.apache.cxf.transport.servlet.CXFServlet
?????? </servlet-class>
?????? <load-on-startup>1</load-on-startup>
??? </servlet>
??? <servlet-mapping>
?????? <servlet-name>CXFServlet</servlet-name>
?????? <!--拦截地址--->
??????? <url-pattern>/service/*</url-pattern>
??? </servlet-mapping>
</web-app>

四。beans.xml
<beans xmlns=" http://www.springframework.org/schema/beans"
??? xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
??? xmlns:jaxws=" http://cxf.apache.org/jaxws"
??? xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
? http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
??<import resource="classpath:META-INF/cxf/cxf.xml" />
??? <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
??? <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
??? <!-- 定义一个bean? -->
??? <bean id="hello" class="demo.spring.impl.HelloWorldImpl" />
??? <!--? 发布webservice implementor="#hello" 这个就是指的第一行定义的那个-->
??? <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>
五。HelloWorldImpl.java/HelloWorld.java/MyObject.java
package demo.spring;
import javax.jws.WebParam;
import javax.jws.WebService;
import entity.MyObject;

@WebService
public interface HelloWorld
{
??? String stringInt(
??? ??@WebParam(name="text",targetNamespace=" http://spring.demo/") String text,
??? ??@WebParam(name="num",targetNamespace=" http://spring.demo/") String num
??? );

??? MyObject[] aryMyObjects(
??? ??@WebParam(name="myObjects",targetNamespace=" http://spring.demo/") MyObject[] myObjects
??? );
}
package demo.spring.impl;
import javax.jws.WebService;
import demo.spring.HelloWorld;
import entity.MyObject;
@WebService
public class HelloWorldImpl implements HelloWorld
{
?public String stringInt(String text,String num)
?? {
??System.out.print("stringInt called");
??return text+" too,"+num;
?? }
?
?? public MyObject[] aryMyObjects(MyObject[] myObjects)
?? {
??? MyObject[] ary=new MyObject[myObjects.length];
??? for(int i=0;i<ary.length;i++)
??? {
???? ary[i]=new MyObject();
???? ary[i].setId(myObjects[i].getId()+i);
???? ary[i].setName(myObjects[i].getName()+" "+i);
??? }
??? return ary;
?? }
}
package entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyObject",propOrder = {
??? "id",
??? "name"
})
public class MyObject
{
??? private int id;
??? private String name;
??? public int getId()
??? {
??? ?return id;
??? }
??? public void setId(int id)
??? {
??? ?this.id = id;
??? }
??? public String getName()
??? {
??? ?return name;
??? }
??? public void setName(String name)
??? {
??? ?this.name = name;
??? }
}

。。。。。。~~OK~~服务端搞定,用tomcat启动webserviceTest。
在IE里面输入 http://localhost:8080/service/HelloWorld?wsdl
可以看到

就说明服务端对了。
现在可以用客户端来测试:
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class ClientTest
{
?public static void main(String[] args)
?{
??testClient();
?}
?
?public static void testClient()
?{
??try
??{
???Service service = new Service();
???Call call = (Call) service.createCall();
???call.setTargetEndpointAddress(" http://localhost:8080/service/HelloWorld?wsdl");
???//stringInt:服务端的方法名
???call.setOperationName(new QName(" http://spring.demo/","stringInt"));
???//传值参数的定义
???call.addParameter(new QName(" http://spring.demo/","text"),XMLType.XSD_STRING,ParameterMode.IN);
???call.addParameter(new QName(" http://spring.demo/","num"),ParameterMode.IN); ???call.setUseSOAPAction(true); ???//返回参数的类型 ???call.setReturnType(XMLType.XSD_STRING); ???System.out.println(call.invoke(new Object[] {"i like you","forever"})); ??} ??catch (Exception e) ??{ ???e.printStackTrace(); ??} ?} }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读