首页 > 代码库 > Web Service简单入门例子
Web Service简单入门例子
Web Service简单入门例子
我们一般实现Web Service的方法有很多种。其中我主要使用了CXF Apache插件和Axis 2两种。
Web Service是应用服务商为了解决每个问题而提供的在线服务方案,其主要采用了SOAP(Simple Object Access Protocol)协议,数据传输格式使用XML格式来描述,因此也具有跨平台的特性。
Web Service是应用服务商为了解决每个问题而提供的在线服务方案,其主要采用了SOAP(Simple Object Access Protocol)协议,数据传输格式使用XML格式来描述,因此也具有跨平台的特性。
web广泛用到的技术:
- TCP/IP:通用网络协议,被各种设备使用
- HTML(标准通用标记语言下的一个应用):通用用户界面,可以使用HTML标签显示数据
- Java:写一次可以在任何系统运行的通用编程语言,因为java具有跨平台特性
- XML(标准通用标记语言下的一个子集):通用数据表达语言,在web上传送结构化数据的容易方法
他们的特点是其开放性,跨平台性,开放性正是Web services的基础。
下面是使用CXF Apache的插件实现Web Service的一个简单入门实例
1========新建一个服务接口
package com.clark;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
public String sayHello(@WebParam(name="name")String name);
public int plus(int a,int b);
}
2========服务接口实现类
package com.clark.impl;
import com.clark.IHelloWorld;
public class HelloWorldImpl implements IHelloWorld {
@Override
public String sayHello(String name) {
return "Hello Wolrd ,"+name;
}
@Override
public int plus(int a, int b) {
return a+b;
}
}
3============服务端
package com.clark.service;
import javax.xml.ws.Endpoint;
import com.clark.impl.HelloWorldImpl;
public class WebServiceApp {
public static void main(String[] args) {
System.out.println("web service start");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/IHelloWorld";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}
4============客户端(下面主要是针对Java普通程序的)
package com.clark.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.clark.IHelloWorld;
public class HelloWorldClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(IHelloWorld.class);
svr.setAddress("http://localhost:8080/CXFWebService/service/IHelloWorld");
IHelloWorld hw = (IHelloWorld) svr.create();
String name = hw.sayHello(" CXF Apache implements Web Service");
int result = hw.plus(2, 3);
System.out.println(name);
System.out.println(result);
}
}
4==============客户端(针对Spring中集成Web Service的Web开发)
package com.clark.web;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.clark.IHelloWorld;
public class HelloWorldClient {
public static void main(String[] args) {
System.out.println("Web Service start..........");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IHelloWorld helloWorld = (IHelloWorld)context.getBean("client");
String name = helloWorld.sayHello("1111111111");
int result = helloWorld.plus(3, 4);
System.out.println(name+" "+result);
System.out.println("Web Service end..........");
}
}
5============Spring 的 applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<jaxws:endpoint
id="helloWorld"
implementor="com.clark.impl.HelloWorldImpl"
address="/IHelloWorld" />
<bean id="client" class="com.clark.IHelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value=http://www.mamicode.com/"com.clark.IHelloWorld"/>
<property name="address"
value=http://www.mamicode.com/"http://localhost:8080/CXFWebService/service/IHelloWorld"/>
</bean>
</beans>
6=============Spring中集成Web Service服务(CXF Servlet的配置),web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CXFWebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:applicationContext.xml</param-value> -->
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</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>
7=============启动服务,地址栏输入
http://localhost:8080/CXFWebService/service/IHelloWorld?wsdl能够看到相应的SOAP协议规范就OK
下面是使用CXF Apache的插件实现Web Service的一个简单入门实例
1========新建一个服务接口
package com.clark;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
public String sayHello(@WebParam(name="name")String name);
public int plus(int a,int b);
}
2========服务接口实现类
package com.clark.impl;
import com.clark.IHelloWorld;
public class HelloWorldImpl implements IHelloWorld {
@Override
public String sayHello(String name) {
return "Hello Wolrd ,"+name;
}
@Override
public int plus(int a, int b) {
return a+b;
}
}
3============服务端
package com.clark.service;
import javax.xml.ws.Endpoint;
import com.clark.impl.HelloWorldImpl;
public class WebServiceApp {
public static void main(String[] args) {
System.out.println("web service start");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/IHelloWorld";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}
4============客户端(下面主要是针对Java普通程序的)
package com.clark.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.clark.IHelloWorld;
public class HelloWorldClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(IHelloWorld.class);
svr.setAddress("http://localhost:8080/CXFWebService/service/IHelloWorld");
IHelloWorld hw = (IHelloWorld) svr.create();
String name = hw.sayHello(" CXF Apache implements Web Service");
int result = hw.plus(2, 3);
System.out.println(name);
System.out.println(result);
}
}
4==============客户端(针对Spring中集成Web Service的Web开发)
package com.clark.web;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.clark.IHelloWorld;
public class HelloWorldClient {
public static void main(String[] args) {
System.out.println("Web Service start..........");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IHelloWorld helloWorld = (IHelloWorld)context.getBean("client");
String name = helloWorld.sayHello("1111111111");
int result = helloWorld.plus(3, 4);
System.out.println(name+" "+result);
System.out.println("Web Service end..........");
}
}
5============Spring 的 applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<jaxws:endpoint
id="helloWorld"
implementor="com.clark.impl.HelloWorldImpl"
address="/IHelloWorld" />
<bean id="client" class="com.clark.IHelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value=http://www.mamicode.com/"com.clark.IHelloWorld"/>
<property name="address"
value=http://www.mamicode.com/"http://localhost:8080/CXFWebService/service/IHelloWorld"/>
</bean>
</beans>
6=============Spring中集成Web Service服务(CXF Servlet的配置),web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CXFWebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:applicationContext.xml</param-value> -->
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</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>
7=============启动服务,地址栏输入
http://localhost:8080/CXFWebService/service/IHelloWorld?wsdl能够看到相应的SOAP协议规范就OK
Web Service简单入门例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。