• springboot拦截json后缀的请求,返回json数据


    需求:请求list.json返回以下数据

    {
        "jsonResult": {
            "code": 200,
            "message": "查询成功",
            "nextUrl": null,
            "data": [
                {
                    "pageNo": 0,
                    "id": 1,
                    "type": "personal",
                    "avatarUrl": null,
                    "source": null,
                    "loginName": "test",
                    "name": "测试人员",
                    "certStatus": 0,
                    "idNum": null,
                    "password": "567",
                    "jobId": "smartTravel",
                    "cellphone": null,
                    "cellphoneActived": false,
                    "email": null,
                    "emailActived": false,
                    "projectName": "测试项目111",
                    "balance": 0,
                    "paymentCode": null,
                    "provinceId": "beijing",
                    "cityId": "xichengqu",
                    "address": "北京市西直门大街3321-1号werer",
                    "enabled": true,
                    "createTime": [
                        2019,
                        9,
                        25,
                        14,
                        11,
                        38
                    ]
                }
            ]
        }
    }

    Controller里面有两个方法

        @PostMapping(value = "list")
        public String list(Model model, String name, Integer certStatus,
                           @RequestParam(required = false) LocalDate startDate,
                           @RequestParam(required = false) LocalDate endDate,
                           Integer pageNo,Integer pageSize) {
    
            HashMap<String,Object> map = new HashMap<>();
            map.put("name",name);
            map.put("certStatus",certStatus);
            map.put("startDate",startDate);
            map.put("endDate",endDate);
    
            Page<ApiUser> list = userService.queryPage(map, pageNo,pageSize);
            model.addAttribute("list", list);
    
            return "user2/list";
        }
    
        @GetMapping(value = "list.json")
        public JsonResult list(String name, Integer certStatus,
                           @RequestParam(required = false) LocalDate startDate,
                           @RequestParam(required = false) LocalDate endDate,
                           Integer pageNo,Integer pageSize) {
    
            HashMap<String,Object> map = new HashMap<>();
            map.put("name",name);
            map.put("certStatus",certStatus);
            map.put("startDate",startDate);
            map.put("endDate",endDate);
    
            Page<ApiUser> list = userService.queryPage(map, pageNo,pageSize);
    
            return successResult("查询成功",list);
        }

    调试的时候每次都进入list.json方法,实际返回结果却是网页代码。也就是返回list方法对应的视图。

    只要进行如下配置就好:

        /**
         * 配置多视图解析器
         *
         * @param manager       manager 会自动构建,configureContentNegotiation可以进行配置
         * @param viewResolvers 当前项目的 viewResolver, (此时会包含上面配置的 freemarkerViewResolver)
         * @return ContentNegotiatingViewResolver
         * @see WebMvcConfigurerAdapter#configureContentNegotiation(org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer)
         */
        @Bean
        public ContentNegotiatingViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager, List<ViewResolver> viewResolvers) {
    
            ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
            viewResolver.setContentNegotiationManager(manager);
    
            // 设置默认view, default view 每次都会添加到 真正可用的视图列表中, json视图没有对应的ViewResolver
            View jackson2JsonView = new MappingJackson2JsonView();
            viewResolver.setDefaultViews(Collections.singletonList(jackson2JsonView));
    
            Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.valueOf("application/json;charset=UTF-8")); //实测需要加上这个,否则之前通过实体属性序列化成json,返回json的方法容易出错。
    
            MappingMediaTypeFileExtensionResolver extensionsResolver = new MappingMediaTypeFileExtensionResolver(mapping);
    
            manager.addFileExtensionResolvers(extensionsResolver);
    
            viewResolver.setViewResolvers(viewResolvers);
            return viewResolver;
        }

    转载自:https://segmentfault.com/a/1190000015721806

    类似文章:请求路径@PathVariable注释中有点.英文句号的问题(忽略英文句号后面的后缀)

    https://www.programcreek.com/java-api-examples/?class=org.springframework.web.accept.ContentNegotiationManager&method=addFileExtensionResolvers
  • 相关阅读:
    Integer to Roman leetcode java
    Reverse Integer leetcode java
    Binary Tree Maximum Path Sum leetcode java
    公司来了一个奇葩需求pppoe client+server+EOIP+vlan
    魔兽数据库-自然
    windows默认dns解析走ipv4而不走ipv6
    ROS支持BCP桥接(基于PPP隧道)
    几款比较好用的电动理发器推荐
    centos 拨号pptp在拨号成功和拨号失败的时候脚本处理!!!非常重要
    ros routeros 脚本命令script
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11648532.html
Copyright © 2020-2023  润新知