在公司完成了一個簡單的rest服務的搭建和調用
1.項目需求
- 創建一個java工程。
- 編寫一個rest服務,要求入參為json字符串,服務可以解析報文並能實現邏輯對比,最后能夠根據對比結果返回正常和異常的情況。
- 通過postman/soapUI工具調用rest服務並進行測試。
標准如下:
1 入參: 2 3 { 4 5 "root":{ 6 7 "loginNo":"noteId" 8 9 }, 10 11 "body":{ 12 13 "busiInfo"{ 14 15 "custId":"noteId", 16 17 "custName":"custName", 18 19 } 20 21 } 22 23 } 24 25 出參: 26 27 { 28 29 "rtnCode":"000000", 30 31 "rtnMsg":"成功" 32 33 } 34 35 注:異常返回 rtnCode=-9999,rtnMsg="程序錯誤"。
2.具體流程
1.環境的配置
1.創建java動態Web項目或者Maven的Web工程
2.首先需要搭建rest服務,需要一些相應的jar包jersey及相關的依賴如下圖:
接下來需要配置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" version="2.5"> <servlet> <servlet-name>UserInfoServlet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.sitech.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>UserInfoServlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
注:搭建rest服務需要使用相應的注解,常用的注解及作用如下:
@javax.ws.rs.ApplicationPath 標識應用路徑,用於由@Path提供的所有資源uri的基本uri。當發布在一個servlet容器中,它的值可以使用web.xml中的servlet-mapping進行重寫。
@javax.ws.rs.Path 標識要請求的資源類或類方法的uri路徑。
@javax.ws.rs.PathParam 將uri中指定的路徑參數綁定到資源方法參數,資源類的字段,或資源類的bean屬性。
@javax.ws.rs.QueryParam 將http請求的Query參數綁定到資源方法參數,資源類的字段,或資源類的bean屬性。
@javax.ws.rs.MatrixParam 將uri矩陣參數的值綁定到資源方法參數,資源類的字段,或資源類的bean屬性。
3.本次項目業務邏輯是要解析json報文,所以需要解析json報文的相關jar包及依賴,如下圖
4.配置好環境后則需編寫代碼實現功能,具體代碼實現如下
1 package com.sitech.rest; 2 3 import javax.servlet.http.HttpServletResponse; 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.QueryParam; 9 import javax.ws.rs.core.Context; 10 import javax.ws.rs.core.MediaType; 11 12 import net.sf.json.JSON; 13 import net.sf.json.JSONObject; 14 15 /************************* 16 * @className RestService.java 17 * @author zyl 18 * @date 2019年1月3日上午8:48:43 19 * @version 1.0.1 20 * @describe 創建一個rest服務,可以解析json對象,分別使用了GET和 21 Post兩種方式進行參數的傳遞 22 ***********************/ 23 24 @Path("jsonChange") 25 public class RestService { 26 27 JSONObject ingredients = new JSONObject(); 28 29 @Context 30 HttpServletResponse response; 31 32 @GET 33 @Produces(MediaType.TEXT_PLAIN) 34 public String getResult(@QueryParam("message") String message) throws Exception { 35 36 response.setCharacterEncoding("UTF-8"); 37 try { 38 JSONObject jsonObj = JSONObject.fromObject(message); 39 40 // 解析root 41 String root = jsonObj.getString("root"); 42 String loginNo = JSONObject.fromObject(root).getString("loginNo"); 43 44 // 解析body 45 String body = jsonObj.getString("body"); 46 String busiInfo = JSONObject.fromObject(body).getString("busiInfo"); 47 String custId = JSONObject.fromObject(busiInfo).getString("custId"); 48 String custName = JSONObject.fromObject(busiInfo).getString("custName"); 49 50 if ("noteId".equalsIgnoreCase(loginNo) && "noteId".equalsIgnoreCase(custId) 51 && "custName".equalsIgnoreCase(custName)) { 52 ingredients.put("rtnCode", "000000"); 53 ingredients.put("rtnMsg", "成功"); 54 System.out.println("成功"); 55 } else { 56 ingredients.put("rtnCode", "-9999"); 57 ingredients.put("rtnMsg", "程序出錯"); 58 System.out.println("錯誤"); 59 } 60 61 return ingredients.toString(); 62 } catch (Exception e) { 63 64 e.printStackTrace(); 65 ingredients.put("rtnCode", "-9999"); 66 ingredients.put("rtnMsg", "程序錯誤"); 67 /* 68 * response.setContentType("text/html;charset=UTF-8"); 69 * response.getWriter().write("json報文格式不正確,出現異常!!!");; 70 */ 71 return ingredients.toString(); 72 } 73 } 74 75 @POST 76 @Produces(MediaType.TEXT_PLAIN) 77 public String postResult(String userInfo) { 78 79 try { 80 81 if ("".equals(userInfo)) { 82 throw new Exception("報文為空異常!!!"); 83 } 84 JSONObject jsonObj = JSONObject.fromObject(userInfo); 85 86 // 解析root 87 String root = jsonObj.getString("root"); 88 String loginNo = JSONObject.fromObject(root).getString("loginNo"); 89 System.out.println("loginNo:" + loginNo); 90 91 // 解析body 92 String body = jsonObj.getString("body"); 93 System.out.println("body" + body); 94 String busiInfo = JSONObject.fromObject(body).getString("busiInfo"); 95 System.out.println("busiInfo" + busiInfo); 96 String custId = JSONObject.fromObject(busiInfo).getString("custId"); 97 System.out.println("custId:" + custId); 98 String custName = JSONObject.fromObject(busiInfo).getString("custName"); 99 System.out.println("custName:" + custName); 100 101 if ("zhuyl".equalsIgnoreCase(loginNo) && "zhuyl".equalsIgnoreCase(custId) 102 && "zhuyinlong".equalsIgnoreCase(custName)) { 103 ingredients.put("rtnCode", "000000"); 104 ingredients.put("rtnMsg", "正確的"); 105 } else { 106 ingredients.put("rtnCode", "-9999"); 107 ingredients.put("rtnMsg", "錯誤的報文"); 108 throw new Exception("參數不正確"); 109 } 110 return ingredients.toString(); 111 } catch (Exception e) { 112 e.printStackTrace(); 113 ingredients.put("rtnCode", "-9999"); 114 ingredients.put("rtnMsg", "錯誤的報文"); 115 return ingredients.toString(); 116 } 117 } 118 }
5.使用SoapUI工具進行測試
運行該Web項目,將url復制下來,打開SoapUI工具進行測試:project--選擇New REST Project,可以給工程起一個名字。選擇路徑:在新建的Project中選擇New REST Service from URI,填寫IP地址和端口號,填寫路徑、選擇方法、提交參數,Resource中填寫具體路徑,在method處選擇實現方式(get/put/post),需要提交參數的可以在左邊直接添加參數名稱和參數值。點擊三角號即可運行測試用例,並能看到返回結果。
注意:在使用SoapUI時返回的參數中文出現亂碼。解決: 在Request的Encoding位置下拉選擇UTF-8編碼格式,再注意在以json格式填寫參數的時候,必須使用UTF-8編碼格式。
正確報文的情況:
參數不正確的情況:
自定義的異常,控制台會捕捉到異常:
輸入格式不正確的json報文控制台也會捕捉到異常,顯示json解析錯誤