Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring使用数组查询参数响应400_Spring_Spring Boot_Spring Mvc - Fatal编程技术网

Spring使用数组查询参数响应400

Spring使用数组查询参数响应400,spring,spring-boot,spring-mvc,Spring,Spring Boot,Spring Mvc,我有以下spring boot@RestController请求映射: @RequestMapping({"/api/blog"}) @RestController public class BlogController { @RequestMapping(value = "/test", method = RequestMethod.GET) public Iterable<Blog> filterBy( @RequestParam(required

我有以下spring boot@RestController请求映射:

@RequestMapping({"/api/blog"})
@RestController
public class BlogController {

   @RequestMapping(value = "/test", method = RequestMethod.GET)
   public Iterable<Blog> filterBy(
        @RequestParam(required = false, name = "filter") String filterStr,
        @RequestParam(required = false, name = "range") String rangeStr,
        @RequestParam(required = false, name="sort") String sortStr) {

           ...
   }
}
但是,提供任何数组查询参数(范围和/或排序)都会导致400的响应,除了“HTTP状态400-错误请求”之外没有其他详细信息

仅使用文件管理器查询parm发出请求是有效的。使用空值添加范围和/或排序有效。我一加上括号[]就好像失败了

我已尝试添加以下异常处理程序来调试控制器和ControllerAdvice类的问题:

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(HttpMessageNotReadableException e) {
  logger.warn("Returning HTTP 400 Bad Request", e);
}
但是,这不会被触发。 我怀疑框架中发生了什么事情,在它到达控制器之前导致了400


感谢您的帮助

您必须对参数进行URL编码!测试时,应该对过滤器、范围和排序参数进行URL编码。请尝试以下操作:

 http://my.api.url/api/blog/test?sort%3D%5B%27title%27%2C%27ASC%27%5D%26range%3D%5B0%2C+24%5D%26filter%3D%7Btitle%3A%27bar%27%7D

尝试将参数定义为
列表
,不要使用括号

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Iterable<Blog> filterBy(
        @RequestParam(required = false, name = "filter") List<String> filterStr,
        @RequestParam(required = false, name = "range") List<String> rangeStr,
        @RequestParam(required = false, name = "sort") List<String> sortStr) {

     filterStr.forEach(s -> System.out.print(", "+ s));
     System.out.println();
     rangeStr.forEach(s -> System.out.print(", "+ s));
     System.out.println();
     sortStr.forEach(s -> System.out.print(", "+ s));
     System.out.println();
     return new ArrayList<>();
    }

    // test url with mockmvc  
    @Test
    public void filterBy() throws Exception {
        mockMvc.perform(get("/test?filter=1,2,3,4&range=5,7,8&sort=desc"))
            .andExpect(status().is2xxSuccessful());
    }

    @Test
    public void filterBy() throws Exception {
       mockMvc.perform(get("/test?filter=1&filter=2&filter=3&filter=4&range=[5,7,8]&sort=desc"))
            .andExpect(status().is2xxSuccessful());
    }
第二个测试打印:

, 1, 2, 3, 4
, 5, 7, 8
, desc
, 1, 2, 3, 4
, [5, 7, 8] // brackets dont seem to work that good
, desc

你能跟踪堆栈吗?但愿我能。响应为400,但不会引发任何异常,这使得调试非常困难。是的,对查询参数进行编码可以按照您提供的方式工作。我使用的是postman,我认为它对所有内容都进行了编码,但似乎没有对值进行编码。我使用的是一个前端库,它定义了查询参数,无法为此项目更改它们。
, 1, 2, 3, 4
, [5, 7, 8] // brackets dont seem to work that good
, desc