HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象,或将Java对象转换为响应报文
注意请求体只有在post请求里才拥有,而get请求直接写在了url中
请求报文注解
RequestBody
@RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@RequestBody进行标识,当前请求的请求体就会为当前注解所标识的形参赋值
1 2 3 4 5
| <form th:action="@{/test}" method="post"> <input type="text" name="name" value="name"> <input type="text" name="password" value="password"> <input type="submit" value="提交"> </form>
|
1 2 3 4 5
| @RequestMapping("/test") public String test(@RequestBody String body){ System.out.println(body); return "target"; }
|
ResponseBody
普通响应方法
1
| <a th:href="@{/test}">servletapi跳转</a>
|
1 2 3 4
| @RequestMapping("/test") public void test(HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.getWriter().print("你好"); }
|
mvc方法响应
1 2 3 4 5 6
| @RequestMapping("/test") @ResponseBody public String test() { return "hi"; }
|
响应对象
但是如果我们响应对象的话,直接返回一个对象,浏览器对这种情况是不能处理的,这时候需要将数据转换为json格式
导入json包
1 2 3 4 5 6
| <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.2.2</version> </dependency>
|
配置Java信息
1 2 3 4 5
| @RequestMapping("/test") @ResponseBody public User test() { return new User("zss","2001","男",20,"200@qq.com"); }
|
开启驱动
1 2
| <mvc:annotation-driven/>
|
如果加载失败,注意是否将json包出现在工件中,单单maven导入可能还是会出现问题,包会自动转换为json的字符串
RequestEntity
1 2 3 4 5 6 7
| @RequestMapping("/test") public String test(RequestEntity<String> entity){ System.out.println(entity); System.out.println(entity.getBody()); return "target"; }
|
ResponseEntity
SpringMVC处理ajax
我们要设置点击链接而不进行跳转,只是在本页进行局部的显示
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
| <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>主页面</title> <script language="JavaScript" th:src="@{/static/js/vue.js}"></script> <script language="JavaScript" th:src="@{/static/js/axios.js}"></script> </head> <body> <div id="app"> <a @click="testAjax" th:href="@{/test}">test</a> </div> <script type="text/javascript"> new Vue({ el:"#app", methods:{ testAjax:function (event){ event.preventDefault(); axios({ method: "post", url:event.target.href, params:{ username:"zss", password:"123" } }).then(function (response){ alert(response.data) }); } } }) </script> </body> </html>
|
1 2 3 4 5 6
| @RequestMapping("/test") @ResponseBody public String test(String username,String password) { System.out.println(username+password); return username; }
|
@RestController注解
@RestController注解是springMVC提供的一个复合注解,标识在控制器的类上,就相当于为类添加了@Controller注解,并且为其中的每个方法添加了@ResponseBody注解
文件上传与下载
文件下载
1 2 3 4 5 6 7 8 9 10 11
| <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>主页面</title>
</head> <body> <a th:href="@{/testDown}">下载</a> </body> </html>
|
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
| @RequestMapping("/testDown") public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException { ServletContext servletContext = session.getServletContext(); String realPath = servletContext.getRealPath("/static/img/夜空.png"); InputStream is = new FileInputStream(realPath);
byte[] bytes = new byte[is.available()]; is.read(bytes); MultiValueMap<String, String> headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename=天空.png"); HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode); is.close(); return responseEntity; }
|
文件上传
上传不能使用get功能,只能使用post请求
添加依赖
1 2 3 4 5 6
| <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
|
文件解析器进行解析,并封装为MultipartFile对象
1 2
| <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
|
设置响应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @RequestMapping("/textup") public String textUp(MultipartFile photo, HttpSession session) throws IOException { String fileName=photo.getOriginalFilename(); ServletContext servletContext=session.getServletContext(); String path=servletContext.getRealPath("/static/img"); File file=new File(path); if(!file.exists()){ file.mkdir(); } String finalPath=path+File.separator+fileName; photo.transferTo(new File(finalPath)); return "target"; }
|
最终上传到out目录下,但是演示的到了target目录下,总感觉哪里配置错误了,插眼!!!
文件重名的问题
如果文件重名
1 2 3 4
| String suffixName=fileName.substring(fileName.lastIndexOf(".")); String uuid= UUID.randomUUID().toString(); fileName=uuid+suffixName;
|