Restful架構思想

java作為一門後端語言,其厲害之處在於web,大家比較熟知的各種網絡應用,java都能做,那麼在這個移動優先的時代,如何繼續發揮java的強大呢。通常是讓java作為一個app的服務端,為app客戶端提供數據,做業務邏輯,所以我們用java來寫接口,app客戶端訪問接口返回json文件進行解析,最後實現業務邏輯。這種方式就是我們通常所說的restful架構風格的api。

restful是一種架構思想,最初由Roy T. Fielding(HTTP/1.1協議專家組負責人)在其2000年的博士學位論文中提出。HTTP就是該架構風格的一個典型應用,其核心思想就是前後端分離,前端通過http請求,如www.xxxx.com/demo/username/password 來訪問後端的接口,然後後端將處理好的數據封裝為json返回,這樣,後端只需關注具體邏輯 提供接口,而前端只關心界面,提高了程序解耦性。 在移動優先的時代,restful極為重要。通常一套後臺可以讓多種終端訪問,包括移動端,pc端。通過restful改進的mvc 在java中比較容易實現restful的是SpringMVC框架,他提供了一套處理json的註解。通過@ResponseBody返回json數據,通過@ResquestBody解析json。

Restful架構思想

下面是一個ios訪問我的java後臺demo,java後臺採用了springMVC和Hibernate。

//java端:

 1 package cotroller;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.List;
6
7 import javax.servlet.http.HttpServletRequest;
8
9 import jdk.nashorn.api.scripting.JSObject;
10 import model.Student;
11 import model.Teacher;
12
13 import org.springframework.stereotype.Controller;
14 import org.springframework.ui.Model;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.RequestBody;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.ResponseBody;
20
21
22
23 import dao.Get;
24 import dao.StudentDAO;
25
26 //登陸servlet
27 @Controller
28 public class LoginCotroller {
29 /**
30 * 1. value="/doLogin/{username}/{password}" 攔截 xxx/doLogin/xx/xx
31 * 2. @ResponseBody 使用此註解將返回數據類型封裝json
32 * 3. @PathVariable("username") 截取請求1.value中{username}的值
33 * 4. Map<string> 服務端將值放入map中再封裝為json,客戶端方便通過key取出value
34 */
35
36 StudentDAO studentDAO = new StudentDAO();//調用登陸判斷方法
37

38 @RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
39 @ResponseBody
40 public Map<string> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){
41 System.out.println("攔截了客戶端json請求");
42 Map<string> map = new HashMap<string>();
43
44 if(studentDAO.loginByStudent(username, password)){
45 System.out.println("密碼正確");
46 map.put("result", "1");
47 return map; //封裝為json返回給客戶端
48 }
49
50 System.out.println("密碼錯誤");
51 map.put("result", "0");
52 return map; //封裝為json返回給客戶端
53 }
54
55 }
/<string>/<string>/<string>/<string>

//ios端:

 1 #import <foundation>
2 #import <stdio.h>
3
4 int main(int argc, const char * argv[]) {
5 @autoreleasepool {
6
7 char oldUsername[128];
8 char oldPassword[128];
9
10 NSLog(@"請輸入用戶名 :");
11 scanf("%s", oldUsername);
12 NSString *username = [NSString stringWithUTF8String:oldUsername]; //轉換為NSString *
13 NSLog(@"請輸入密碼 :");
14 scanf("%s", oldPassword);
15 NSString *password = [NSString stringWithUTF8String:oldPassword]; //轉換為NSString *
16
17 //訪問springMVC後臺並解析返回的json數據
18 //定義一個異常
19 NSError *error;
20
21 //定義請求action 使用stringWithFormat拼接字符串

22 NSString *url = [NSString stringWithFormat:@"http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@", username, password];
23
24 //加載一個NSURL對象
25 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
26
27 //發送請求 將請求的url數據放到NSData對象中
28 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
29
30 //NSJSONSerialization從response請求中解析出數據放到字典中
31 NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
32
33 NSString *resultValue = [jsonResult objectForKey:@"result"];
34
35 NSLog(@"你的url是%@", url);
36 NSLog(@"服務端返回值%@", resultValue);
37
38 // oc字符串比較方法 resultValue isEqualToString:@"1"] 和java 的equlse類似
39 if([resultValue isEqualToString:@"1"]){
40 NSLog(@"登錄成功!");
41 }else{
42 NSLog(@"登錄失敗,用戶名或密碼錯誤!");
43 }
44
45
46 }
47 return 0;
48 }
/<stdio.h>/<foundation>

fight!!!!!!!!


分享到:


相關文章: