标签:
可用的框架有不少,我用的是jersey。
直接上代码,其实,如果你会web service 这个restful的就很好理解了,自己跑一遍就OK了
用到的类
User.java
1 package demo.helloworld; 2 3 import javax.xml.bind.annotation.XmlRootElement; 4 5 /** 6 * @author edi_kai 7 * @version 创建时间:2015-8-20 下午03:46:24 8 * 类说明 9 */ 10 @XmlRootElement(name="User") 11 public class User { 12 13 private String userName; 14 private String sex; 15 private int age; 16 17 public User(String userName, String sex, int age) { 18 super(); 19 this.userName = userName; 20 this.sex = sex; 21 this.age = age; 22 } 23 24 public User() { 25 super(); 26 } 27 28 public String getUserName() { 29 return userName; 30 } 31 public void setUserName(String userName) { 32 this.userName = userName; 33 } 34 public String getSex() { 35 return sex; 36 } 37 public void setSex(String sex) { 38 this.sex = sex; 39 } 40 public int getAge() { 41 return age; 42 } 43 public void setAge(int age) { 44 this.age = age; 45 } 46 }
RestfulService.java
1 package demo.helloworld; 2 3 import javax.ws.rs.Consumes; 4 import javax.ws.rs.GET; 5 import javax.ws.rs.POST; 6 import javax.ws.rs.Path; 7 import javax.ws.rs.Produces; 8 import javax.ws.rs.core.MediaType; 9 10 /** 11 * @包名 demo.RestfulService 12 * @文件名 RestfulService.java 13 * @作者 edi_kai 14 * @创建日期 2015年8月20日 15 * @版本 V 1.0 16 * @描述 17 */ 18 @Path("/service") 19 public class RestfulService{ 20 21 final String XMLNS_NAMESPACE = "http://www.cnblogs.com/edi-kai/service"; 22 final String ROOT_NODE = "root"; 23 24 @GET 25 @Produces("application/json") 26 @Path("/json") 27 public User getJsonMessage() { 28 User user = new User("admin", "BeiJing", 25); 29 return user; 30 } 31 32 @GET 33 @Produces(MediaType.APPLICATION_XML) 34 @Path("/xml") 35 public User getXmlMessage() { 36 User user = new User("admin", "BeiJing", 25); 37 return user; 38 } 39 40 @POST 41 @Produces(MediaType.APPLICATION_JSON) 42 @Consumes(MediaType.APPLICATION_JSON) 43 @Path("/getUser") 44 public User getMessageByJson(User user) { 45 System.out.println(user.getUserName()); 46 return user; 47 } 48 49 }
web.xml文件配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 8 <servlet> 9 <servlet-name>rsServlet</servlet-name> 10 <servlet-class> 11 com.sun.jersey.spi.container.servlet.ServletContainer 12 </servlet-class> 13 14 <!-- 不加这个,格式转换会有问题,比如JSON/XML --> 15 <init-param> 16 <param-name>javax.ws.rs.core.Application</param-name> 17 <param-value>javax.ws.rs.core.Application</param-value> 18 </init-param> 19 20 <load-on-startup>1</load-on-startup> 21 22 </servlet> 23 24 <servlet-mapping> 25 <servlet-name>rsServlet</servlet-name> 26 <url-pattern>/rest/*</url-pattern> 27 </servlet-mapping> 28 29 30 <welcome-file-list> 31 <welcome-file>index.jsp</welcome-file> 32 </welcome-file-list> 33 34 </web-app>
最后是调用的代码,我这里为了省事,直接在JSP里面用ajax调用的,代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘index.jsp‘ starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <script type="text/javascript" 19 src="<%=request.getContextPath()%>/js/jquery-1.9.1.js"></script> 20 <script type="text/javascript" 21 src="<%=request.getContextPath()%>/js/json2.js"></script> 22 </head> 23 <body> 24 This is my JSP page. 25 <br> 26 </body> 27 28 <script type="text/javascript"> 29 $(function(){ 30 /* 31 alert(JSON.stringify( 32 {"productName":"Java编程思想(第4版)", 33 "isbn":"9787111213826", 34 "author":"[美]埃克尔", 35 "publishDate":1183270161125} 36 )); 37 */ 38 var url = "rest/helloworld/getUser"; 39 var url_1 = "http://10.169.217.198:5000/wa_cancel"; 40 41 var param = JSON.stringify( 42 {"userName":"Java编程思想(第4版)", 43 "sex":"男", 44 "age":25} 45 ); 46 var param_1 = JSON.stringify( 47 {‘WithdrawID‘: ‘APP2015062610000003‘, 48 ‘TranTime‘: ‘1438589070‘, 49 ‘Uid‘: ‘100163‘, 50 ‘Sign‘: ‘599ba17c1b882d0eb57c22572c12e006‘} 51 ); 52 $.ajax({ 53 type : "POST", 54 url : url_1, 55 dataType : "json", 56 contentType : "application/json", 57 data : param_1, 58 success : function(data){ 59 alert("以下是从服务器返回的User对象:\n\n" + JSON.stringify(data)); 60 }, 61 error : function(jqXHR, textStatus, errorThrown){ 62 console.info(‘error: ‘ + textStatus+‘,‘+jqXHR+‘,‘+errorThrown); 63 } 64 }); 65 }); 66 </script> 67 </html>
可能会用到的jar包,我添加了这些,但可能有些是用不到的,注意要讲其中的asm-3.3.1.jar、jersey-bundle-1.9.1.jar、jettison-1.1.jar、jsr311-api-1.1.1.jar这四个jar包放到tomcat的lib目录下才可以
最后附上jar包的下载地址jar.zip
标签:
原文地址:http://www.cnblogs.com/edi-kai/p/4745894.html