Spring MVC----@ResponseBody注解(json)

博客主要介绍Java相关知识,包括@ResponseBody注解,它能将数据转成HttpOutputMessage并封装成json返回;@RequestBody注解可将请求数据转为String;还提及文件上传、下载操作,以及使用@JsonIgnore注解排除字段、给json数据起别名等功能。

1、@ResponseBody注解

该注解会去找HttpMessageConverter<T>,将需要返回的数据转变成HttpOutputMessage,返回给浏览器

 

该注解作用一般可以将数据封装成json格式的数据返回回去

 

Maven

        <!-- Json Begin -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
 <!-- Json End -->

  

其他功能

1、如果不需要某个字段被打包的话

使用@JsonIgnore注解get方法上

前提必须有这个依赖(上面已经有了)

 <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
</dependency>

 

2、给json数据(封装时候)起一个别名

 

 

 

 

2、@RequestBody注解

将请求的数据转化成String

controller

    @RequestMapping(value = "/filetest",method = RequestMethod.POST)
    public String filetest(@RequestBody String string){
        try {
            String s2 = new String(string.getBytes("iso-8859-1"), "utf8");
            System.out.println(s2);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "login";
    }

html

<form action="/filetest" method="post" enctype="multipart/form-data">
    <input type="file" name="string">
    <input type="submit" value="提交">
</form>

  

 

文件上传

导入jar包

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>

Controller

    @RequestMapping(value = "/fileupload",method = RequestMethod.POST)
    public String fileupload(MultipartFile file,HttpServletRequest httpServletRequest) throws IOException {
        //获取文件的名字
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String path = httpServletRequest.getSession().getServletContext().getRealPath("/")+originalFilename;
        FileOutputStream fileOutputStream = new FileOutputStream(path,true);
        int len=-1;
        byte[] bytes = new byte[1024];
        while ((inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes);
        }
        fileOutputStream.close();
        inputStream.close();
        return "login";
    }

  

 

3、ResponseEntity

文件下载

    @RequestMapping(value = "/filedownload",method = RequestMethod.GET)
    public ResponseEntity filedownload(HttpServletRequest httpServletRequest) throws IOException {
        //设置响应体
        byte[] body = null;
        ServletContext servletContext = httpServletRequest.getSession().getServletContext();
        InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/abc.txt");
        body = new byte[resourceAsStream.available()];
        resourceAsStream.read(body);
        resourceAsStream.close();

//        方式二,设置响应体
//        System.out.println(httpServletRequest.getSession().getServletContext().getRealPath("/"));
//        String filepath = httpServletRequest.getSession().getServletContext().getRealPath("/")+"WEB-INF\\abc.txt";
//        FileInputStream fileInputStream = new FileInputStream(filepath);
//        body = new byte[fileInputStream.available()];
//        fileInputStream.read(body);
//        fileInputStream.close();

        //设置响应头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Disposition","attachment;filename=abc.txt");

        //设置响应码
        HttpStatus httpStatus = HttpStatus.OK;
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(body,httpHeaders,httpStatus);

        return responseEntity;
    }

转载于:https://www.cnblogs.com/yanxiaoge/p/10898299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值