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。
//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返回给客户端
}
}
//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;
}