webservice是跨域请求的一种,脚本平台需要xml和http,下面我写个demo来给大家说一下cxf的webservice,cxf是apache的,是xfire的升级。适合初学者。
(一)java的实现
1.建立一个web项目。
2.创建实体类
public class User{
private String name;
private String password;
public User(){}
public User(String name,String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString(){
return "name:"+name+",password:"+password;
}
}
3.创建你的服务接口
@WebService
public interface WsTest {
public User getUser(@WebParam(name="name") String name,@WebParam(name="password") String password);
}
注解是必须加的
4.服务实现类
@WebService(endpointInterface="cn.sxy.servcie.WsTest",serviceName="userService")
public class WsTestImpl implements WsTest{
public User getUser(@WebParam(name="name") String name,@WebParam(name="password") String password) {
return new User(name,password);
}
}
注解是必须加的
5.先说java实现服务,下面创建一个类,用来启动你的服务
public class Test {
public static void main(String[] args) {
WsTestImpl userService = new WsTestImpl();
Endpoint.publish("http://localhost:8083/cxfws",userService);
System.out.println("服务已启动.........");
}
}
run之后你就得在你的浏览器上输入网址然后加上wsdl,这样的http://localhost:8083/cxfws?wsdl;因为wsdl是webservice的描述语言,看让你直观的看到生成的文件,顺便说一下webservice的三个元素:SOAP (简易对象访问协议),UDDI (通用描述、发现及整合),WSDL (Web services 描述语言)
6.测试你的服务
public class ClientTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(WsTest.class);
factoryBean.setAddress("http://localhost:8083/cxfws?wsdl");
WsTest UserService = (WsTest)factoryBean.create();
User user = UserService.getUser("xiao","123");
System.out.println("User的返回值:"+user);//将来你的服务就是这样返回给调用你服务的人
}
}
以上就是java的实现,但是我开头提到了建一个web项目所以还有就是使用服务器发布。
(二)web的实现
7.和上面一样只不过配置一个web.xml,和一个spring.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>cxfws</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>WEB-INF/spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 创建你的服务网址 --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
spring.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"> <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" /> <jaxws:endpoint id="userServicces" implementor="cn.sxy.servcie.WsTestImpl" address="/userServicces" /> </beans>
启动你的服务器
输入你的网址:以我的示例http://localhost:8080/cxfws/
点进去就能看见和上面一样的wsdl文件,让那后测试只要把网址换一下即可,就会看见一样的效果。
代码的网址:http://www.zuidaima.com/share/3653996184292352.htm
最近浏览





