json数据交互
客户端发送请求一共2种方式json回来
1.key value 原生js请求
2.json方式
html---------->Controller
以下有2种方式
1.请求json过渡到过滤器
2.过滤器如何快速返json
这个是通过url地址来传值
http://localhost:8080/TestSpringMvc/ajax/test1?empno=1&empname=2&sal=3
@Controller
@RequestMapping("/ajax")
public class AjaxController {
/*
* content-type:application/x-www-form-urlencoded=> 1.test(Integer empno, String empname, Double sal)
* content-type:application/json => @RequestBody Employee e
* @RequestBody:解析json字符串,把请求数据拿出来,放在对象里
*
*/
//http://localhost:8080/TestSpringMvc/ajax/test1?empno=1&empname=2&sal=3
//test1 通过一个一个传递属性来获得
@RequestMapping("/test1")
public void test1(Integer empno,String empname,Double sal ,HttpServletResponse resp) {
System.out.println(empno);
System.out.println(empname);
System.out.println(sal);
try {
resp.getWriter().append("{\"result\":true}");
} catch (IOException e) {
e.printStackTrace();
}
}
}
显示地址不一样
第二种方式这个是通过阿贾克斯来传,请求数据组装成(节深)字符串形式向Springmvc发送放到@RequestBody Employee e 字符串形式和解析出来放到这里
com.fasterxml.jackson.core
jackson-databind
2.10.0
Insert title here
@Controller
@RequestMapping("/ajax")
public class AjaxController {
/*
* content-type:application/x-www-form-urlencoded=> 1.test(Integer empno, String empname, Double sal)
* content-type:application/json => @RequestBody Employee e
* @RequestBody:解析json字符串,把请求数据拿出来,放在对象里
*
*/
//http://localhost:8080/TestSpringMvc/ajax/test1?empno=1&empname=2&sal=3
//test1 通过一个一个传递属性来获得
@RequestMapping("/test1")
public void test1(Integer empno,String empname,Double sal ,HttpServletResponse resp) {
System.out.println(empno);
System.out.println(empname);
System.out.println(sal);
try {
resp.getWriter().append("{\"result\":true}");
} catch (IOException e) {
e.printStackTrace();
}
}
//test2 通过前端js框架组装字符串 到这里进行字符串解析 成为类获得值
@RequestMapping("/test2")
public void test(@RequestBody Employee e, HttpServletResponse resp)
{
System.out.println(e.getEmpno());
System.out.println(e.getEmpname());
System.out.println(e.getSal());
try {
resp.getWriter().append("{\"result\":true}");
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
}
数据已经传给Controller 数据如何通过controller传送给页面!
在SpringMvc有个处理,返回是一个对象或者一个集合在方法返回值上面加个
@RequestBody
@RequestMapping("/test3")
@ResponseBody
public List test(@RequestBody Employee e)
{
System.out.println(e.getEmpno());
System.out.println(e.getEmpname());
System.out.println(e.getSal());
List list = new ArrayList<>();
Employee e1 = new Employee();
e1.setEmpno(1);
e1.setEmpname("jack");
Employee e2 = new Employee();
e2.setEmpno(2);
e2.setEmpname("james");
list.add(e1);
list.add(e2);
return list;
}
**还是通过 输入ajax地址返回来还是一个对象
html---->controller springMVC
html向controller走的时候,是组装成json字符串过去的
html----controller 艾克寿司做的
StringMVC解析json字符串组装成对象放到控制器里,然后查询数据库,列表打印成json字符串,把列表打成json字符串发送到客户端来了调用了一个组件
@RequestBody SpringMvc
@ResponseBody json
如果一个controller下,都是响应ajax请求,@RestController = @Controller + @ResponseBody
如何通过字符串返回客户端
package com.fcx.bean;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Employee {
private int empno;
private String empname;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date hiredate;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
}
@RestController
@RequestMapping("/ajax2")
public class AjaxController2 {
/*
* content-type:application/x-www-form-urlencoded=> 1.test(Integer empno, String empname, Double sal)
* content-type:application/json => @RequestBody Employee e
* @RequestBody: 解析json字符串,把请求数据拿出来,放在对象里。
*
*/
@RequestMapping("/test1")
public List test(@RequestBody Employee e)
{
System.out.println(e.getEmpno());
System.out.println(e.getEmpname());
List list = new ArrayList<>();
Employee e1 = new Employee();
e1.setEmpno(1);
e1.setEmpname("jack");
e1.setHiredate(new Date());
Employee e2 = new Employee();
e2.setEmpno(2);
e2.setEmpname("james");
e2.setHiredate(java.sql.Date.valueOf("1987-01-01"));
list.add(e1);
list.add(e2);
return list;
}
}
用阿贾克斯文件上传
Insert title here
@Controller
@RequestMapping("/upload2")
public class UploadController2 {
@RequestMapping("/upload")
public void upload(String username, Date birthday, MultipartFile avatar, HttpServletResponse resp)
{
System.out.println("JAJAJAJAJA");
System.out.println(username);
System.out.println(birthday);
System.out.println(avatar.getOriginalFilename());
String newfilename = UUID.randomUUID().toString()+avatar.getOriginalFilename().substring(avatar.getOriginalFilename().lastIndexOf("."));
File dest = new File("d:/images",newfilename);
try {
avatar.transferTo(dest);
} catch (IllegalStateException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
resp.getWriter().append("{\"result\":true}");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
RESTFUl面向资源一种请求
restui面向资源的,你原来对比一下,现在请求url
get emp查询,set emp ,delete emp
,url加动词体现干啥,你不应该这样写面向资源方式写,你操作员工,你想查询所有员工 emps 配合请求方式get然后你想往这里面新增一个路径emps
只是提交方式post,我想用id查询员工信息 emp/员工编号101或者员工102更像他还是这一个资源
更新 put 删除101 delte 在这种架构下,面向资源,在url路径下不应该有资源配合请求方式
@RestController
public class RestFullController {
@GetMapping("/emps")
public List getEmps()
{
List list = new ArrayList<>();
Employee e = new Employee();
e.setEmpno(1);
e.setEmpname("jack");
e.setHiredate(new Date());
Employee e2 = new Employee();
e2.setEmpno(2);
e2.setEmpname("james");
e2.setHiredate(java.sql.Date.valueOf("1990-1-1"));
list.add(e);
list.add(e2);
return list;
}
@PostMapping("/emps")
public String addEmp(Employee e)
{
System.out.println(e.getEmpno());
System.out.println(e.getEmpname());
System.out.println(e.getHiredate());
//add to database
return "{\"result\":true}";
}
@GetMapping("/emp/{id}")
public Employee getEmployee(@PathVariable int id)
{
System.out.println(id);
return new Employee();
}
@DeleteMapping("/emp/{id}")
public String deleteEmployee(@PathVariable int id)
{
System.out.println(id);
return "{\"result\":true}";
}
@PutMapping("/emp/{id}")
public String updateEmployee(@PathVariable int id, Employee e)
{
System.out.println(id);
System.out.println(e.getEmpno());
System.out.println(e.getEmpname());
System.out.println(e.getHiredate());
//add to database
return "{\"result\":true}";
}
}
加个过滤器
HttpMethodFilter
org.springframework.web.filter.HttpPutFormContentFilter
HttpMethodFilter
/*