java作為一門后端語言,其厲害之處在于web,大家比較熟知的各種網絡應用,java都能做,那么在這個移動優先的時代,如何繼續發揮java的強大呢。通常是讓java作為一個app的服務端,為app客戶端提供數據,做業務邏輯,所以我們用java來寫接口,app客戶端訪問接口返回json文件進行解析,最后實現業務邏輯。
而這種方式我們通常叫做restful。
restful是一種架構思想,是一位博士生在N年前發表的一篇博士生論文,其核心思想就是前后端分離,前端通過http請求,如www.xxxx.com/demo/username/password 來訪問后端的接口,然后后端將處理好的數據封裝為json返回,這樣,后端只需關注具體邏輯 提供接口,而前端只關心界面,提高了程序解耦性。 在移動優先的時代,restful極為重要。通常一套后臺可以讓多種終端訪問,包括移動端,pc端。 通過restful改進的mvc 在java中比較容易實現restful的是SpringMVC框架,他提供了一套下面是一個ios訪問我的java后臺demo,java后臺采用了springMVC和Hibernate。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
//java端 package cotroller; import java.util.HashMap; import java.util.Map; import java.util.List; import javax.servlet.http.HttpServletRequest; import jdk.nashorn.api.scripting.JSObject; import model.Student; import model.Teacher; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import dao.Get; import dao.StudentDAO; //登陸servlet @Controller public class LoginCotroller { /** * 1. value="/doLogin/{username}/{password}" 攔截 xxx/doLogin/xx/xx * 2. @ResponseBody 使用此注解將返回數據類型封裝json * 3. @PathVariable("username") 截取請求1.value中{username}的值 * 4. Map<String, Object> 服務端將值放入map中再封裝為json,客戶端方便通過key取出value */ StudentDAO studentDAO = new StudentDAO(); //調用登陸判斷方法 @RequestMapping (value= "/doLogin/{username}/{password}" ,method=RequestMethod.GET) @ResponseBody public Map<String, Object> getTeacher( @PathVariable ( "username" ) Integer username, @PathVariable ( "password" ) String password){ System.out.println( "攔截了客戶端json請求" ); Map<String, Object> map = new HashMap<String, Object>(); if (studentDAO.loginByStudent(username, password)){ System.out.println( "密碼正確" ); map.put( "result" , "1" ); return map; //封裝為json返回給客戶端 } System.out.println( "密碼錯誤" ); map.put( "result" , "0" ); return map; //封裝為json返回給客戶端 } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
//ios端 #import <Foundation/Foundation.h> #import <stdio.h> int main( int argc, const char * argv[]) { @autoreleasepool { char oldUsername[128]; char oldPassword[128]; NSLog(@ "請輸入用戶名 :" ); scanf ( "%s" , oldUsername); NSString *username = [NSString stringWithUTF8String:oldUsername]; //轉換為NSString * NSLog(@ "請輸入密碼 :" ); scanf ( "%s" , oldPassword); NSString *password = [NSString stringWithUTF8String:oldPassword]; //轉換為NSString * //訪問springMVC后臺并解析返回的json數據 //定義一個異常 NSError *error; //定義請求action 使用stringWithFormat拼接字符串 NSString *url = [NSString stringWithFormat:@ "http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@" , username, password]; //加載一個NSURL對象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; //發送請求 將請求的url數據放到NSData對象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //NSJSONSerialization從response請求中解析出數據放到字典中 NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSString *resultValue = [jsonResult objectForKey:@ "result" ]; NSLog(@ "你的url是%@" , url); NSLog(@ "服務端返回值%@" , resultValue); // oc字符串比較方法 resultValue isEqualToString:@"1"] 和java 的equlse類似 if ([resultValue isEqualToString:@ "1" ]){ NSLog(@ "登錄成功!" ); } else { NSLog(@ "登錄失敗,用戶名或密碼錯誤!" ); } } return 0; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/liaohai/p/6428363.html