SpringBoot之GET请求
2019-03-20 本文已影响0人
满庭花醉三千客
新建一个java类,LoginController:

LoginController.java具体的代码:
package com.bianla.demotestspringboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class LoginController {
//构建一个公开的student对象
public Student student = new Student();
//声明api的路径以及请求方式
//[http://localhost:8080/api/login?username=wangqilong&password=123456](http://localhost:8080/api/login?username=wangqilong&password=123456)
@RequestMapping(value = "api/login",method = RequestMethod.GET)
//从请求参数中获取username的值赋给usrName,拿到请求参数中password的值赋给pwd
public Object loginAction(@RequestParam(value = "username",required = true)String usrName,
@RequestParam(value = "password",required = true)String pwd){
//构建一个返回的map
Map<String, Object> map = new HashMap<String, Object>();
//调用student的studentLogin方法,传入账户、密码
if (student.studentLogin(usrName,pwd)){
//账号密码正确
System.out.println("密码正确");
//自定义的返回值
map.put("result","1");
map.put("message","(王啟龙)账号密码正确,恭喜你");
//data数据内容
Map<String,Object> subMap = new HashMap<String, Object>();
subMap.put("userID","213435");
subMap.put("phone","13566667777");
subMap.put("nickName","Kayle");
subMap.put("Email","[234545@qq.com](mailto:234545@qq.com)");
map.put("data",subMap);
return map;
}
//账号密码错误
System.out.println("密码错误");
map.put("message","(王啟龙)账号或密码错误,请检查之");
map.put("result","0");
map.put("data","null");
return map;
}
}
class Student{
public boolean studentLogin (String name,String password){
if (name.equals("wangqilong") && password.equals("123456")){
return true;
}else {
return false;
}
}
}
run一下,效果:

用户名为wangqilong,密码为123456 我们前往Safari调用(输入错误密码12345):
http://localhost:8080/api/login?username=wangqilong&password=12345
效果:

正确调用:
http://localhost:8080/api/login?username=wangqilong&password=123456

加油~