@ResponseBody返回JSON数据,360安全浏览器弹出下载页面

编程入门 行业动态 更新时间:2024-10-24 22:21:16

文章目录

    • 问题重现
    • 解决方法
    • 成功解决

问题重现

Controller中使用@ResponseBody返回JSON数据。

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(path = "/", method = RequestMethod.GET)
    @ResponseBody
    public List<Student> selectStudents() {
        return studentService.selectStudents();
    }
}

下图:本机运行项目,用360安全浏览器访问,没有问题。

下图:把项目部署到阿里云服务器上,用360安全浏览器访问,不会显示想要展示的JSON字符串,而是会把字符串存到后缀为.json的文件里,下载下来。

下图:把项目部署到阿里云服务器上,用chrome浏览器访问,没有问题。

解决方法

pom.xml 里加上:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

修改Controller代码为:

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(path = "/", method = RequestMethod.GET)
    //@ResponseBody
    public void selectStudents(HttpServletResponse response) {
        //return studentService.selectStudents();
        response.setContentType("text/html;charset=utf-8");
        try (
                PrintWriter writer = response.getWriter();
        ) {
            writer.write(JSON.toJSONString(studentService.selectStudents()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

成功解决

下图:此时本地可以访问。

下图:把项目部署到阿里云服务器上,用360安全浏览器访问,可以显示想要展示的JSON字符串。

下图:把项目部署到阿里云服务器上,用chrome浏览器访问,没有问题。

更多推荐

@ResponseBody返回JSON数据,360安全浏览器弹出下载页面

本文发布于:2023-06-13 18:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出   下载页面   浏览器   数据   ResponseBody

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!