@Controller
public class AlphaController {
@Autowired
private AlphaService alphaService;
@RequestMapping("hello")
@ResponseBody
public String sayHello(){
return "hello spring boot";
}
@RequestMapping("data")
@ResponseBody
public String getData(){
return alphaService.find();
}
@RequestMapping("http")
public void http(HttpServletRequest request , HttpServletResponse response){
//获取请求数据
System.out.println(request.getMethod());
System.out.println(request.getServletPath());
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()){
String name = enumeration.nextElement();
String value = request.getHeader(name);
System.out.println(name + ":" + value);
}
System.out.println(request.getParameter("code"));
//返回响应数据
response.setContentType("text/html;charset=utf-8");
try (PrintWriter writer = response.getWriter()) {
writer.write("<h1>牛客网</h1>");
}catch (IOException e){
e.printStackTrace();
}
}
//GET请求
// /students?current=1&limit=20
@RequestMapping(path = "/students",method = RequestMethod.GET)
@ResponseBody
public String getStudents(
@RequestParam(name = "current",required = false,defaultValue = "1") int current,
@RequestParam(name = "limit",required = false,defaultValue = "10") int limit
){
System.out.println(current);
System.out.println(limit);
return "some student";
}
// /students/123
@RequestMapping(path = "/student/{id}",method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable("id")int id){
System.out.println(id);
return "a student";
}
//POST请求
@RequestMapping(path = "/student",method = RequestMethod.POST)
@ResponseBody
public String saveStudent(String name,int age){
System.out.println(name);
System.out.println(age);
return "success";
}
//响应html数据
@RequestMapping(path = "/teacher",method = RequestMethod.GET)
public ModelAndView getTeacher(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name","张三");
modelAndView.addObject("age",30);
modelAndView.setViewName("/demo/view");
return modelAndView;
}
@RequestMapping(path = "/school",method = RequestMethod.GET)
public String getSchool(Model model){
model.addAttribute("name","北京大学");
model.addAttribute("age",30);
return "/demo/view";
}
//响应json数据(异步请求)
//java对象 -->json字符串 -->js对象
@RequestMapping(path = "/emp",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> getEmp(){
Map<String, Object> emp = new HashMap<>();
emp.put("name","张三");
emp.put("age",90);
emp.put("salary",99999.9);
return emp;
}
@RequestMapping(path = "/emps",method = RequestMethod.GET)
@ResponseBody
public List<Map<String,Object>> getEmps(){
List<Map<String,Object>> list = new ArrayList<>();
Map<String, Object> emp = new HashMap<>();
emp.put("name","张三");
emp.put("age",90);
emp.put("salary",99999.9);
list.add(emp);
emp = new HashMap<>();
emp.put("name","李四");
emp.put("age",90);
emp.put("salary",99999.9);
list.add(emp);
emp = new HashMap<>();
emp.put("name","王五");
emp.put("age",90);
emp.put("salary",99999.9);
list.add(emp);
return list;
}
}
GET请求有两种请求方式,可以理解为服务器从浏览器获取参数
/students?current=1&limit=20
// /students?current=1&limit=20
@RequestMapping(path = "/students",method = RequestMethod.GET)
@ResponseBody
public String getStudents(
@RequestParam(name = "current",required = false,defaultValue = "1") int current,
@RequestParam(name = "limit",required = false,defaultValue = "10") int limit
){
System.out.println(current);
System.out.println(limit);
return "some student";
}
/students/123
// /students/123
@RequestMapping(path = "/student/{id}",method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable("id")int id){
System.out.println(id);
return "a student";
}
对应的传参方式也不相同,使用的注解也不相同
POST请求可以理解为服务器向浏览器提交参数
//POST请求
@RequestMapping(path = "/student",method = RequestMethod.POST)
@ResponseBody
public String saveStudent(String name,int age){
System.out.println(name);
System.out.println(age);
return "success";
}