java rest版简单的webservice
目前的webservice风格,rest应该是其中一种 还有种就是soap,rest是轻量级的,越来越流行。下面举一个简单例子说明下rest的用法。
1. 准备ws的jar和spring的jar,如何要连接数据的话就自行准备这边就不提供了
下载jar地址:http://download.csdn.net/detail/taopeng_100/7827035
或者:http://pan.baidu.com/s/1qWVejtq
下载好jar之后将其放入lib下就可以了。
1. 创建个项目,名叫RestwsTest,配置spring配置文件spring-source.xml
spring-source.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://cxf.apache.org/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <context:annotation-config /> <context:component-scan base-package="com.*" /> <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 id="restSample" class="com.hoo.service.RESTSampleSource" /> <jaxrs:server id="restServiceContainer" address="/rest"> <jaxrs:serviceBeans> <bean class="com.tp.soft.web.ws.impl.LoginServiceImpl" /> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="" value="application/" /> </jaxrs:extensionMappings> <jaxrs:languageMappings> <entry key="en" value="en-gb"></entry> </jaxrs:languageMappings> <jaxrs:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> </jaxrs:providers> </jaxrs:server> </beans>
2. 配置web.xml文件
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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>RestwsTest</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-source.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> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> </web-app>
3. 编写接口LoginService
LoginService.java如下:
package com.tp.soft.web.ws; import java.util.Map; import javax.jws.WebService; @WebService public interface LoginService { public abstract Map<String, Object> doLogin(String username, String password); }
4. 编写接口的实现类LoginServiceImpl
LoginServiceImpl.java如下:
package com.tp.soft.web.ws.impl; import java.util.HashMap; import java.util.Map; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.tp.soft.web.ws.LoginService; @Path("/loginService") public class LoginServiceImpl implements LoginService { @POST @Path(value = "/login") @Produces(MediaType.APPLICATION_JSON) public Map<String, Object> doLogin( @FormParam(value = "username") String username, @FormParam(value = "password") String password) { Map<String, Object> map = new HashMap<String, Object>(); map.put("user", username + password); return map; } }
此时可以在本地直接通过浏览器访问地址:http://localhost:8080/RestwsTest/ws/rest/loginService /login?username="zs"&password="123"
特别注意这边访问的是通过get的 如果要能访问就要将@POST改成@GET
4. 服务器写好了,下面就来写客服端的代码,客户端主要就是获取user对象的json,代码如下:
RestClient.java如下:
package com.tp.soft.client; import java.io.IOException; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class RestClient { public static void main(String[] args) { String url = "http://localhost:8080/RestwsTest/ws/rest/loginService/login"; HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); NameValuePair[] data = { new NameValuePair("username", "zs"), new NameValuePair("password", "123") }; method.setRequestBody(data); try { int statusCode = client.executeMethod(method); if (statusCode == 200) { String strJson = method.getResponseBodyAsString(); System.out.println(strJson); // System.out.println(map.get("user").getUsername()); } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
注意上面代码里面的PostMethod,在LoginServiceImpl里面的方法必须是@POST才可以,如果是@GET或者其他会报错的。
怀有希望!!