相关疑难解决方法(0)

将Map <String,String>传递给springMVC控制器

我试图从ajax发送HashMap或任何其他Map实现到Spring MVC控制器

这是我如何做的细节:

Ajax调用如下

var tags = {};
tags["foo"] = "bar";
tags["whee"] = "whizzz";


$.post("doTestMap.do",   {"tags" : tags }, function(data, textStatus, jqXHR) {
if (textStatus == 'success') {
    //handle success
    console.log("doTest returned " + data);
} else {
    console.err("doTest returned " + data);
}
}); 
Run Code Online (Sandbox Code Playgroud)

那么在控制器方面我有:

@RequestMapping(value="/publisher/doTestMap.do", method=RequestMethod.POST)
public @ResponseBody String doTestMap(@RequestParam(value = "tags", defaultValue = "") HashMap<String,String> tags, HttpServletRequest request) {  //

    System.out.println(tags);

    return "cool";
} 
Run Code Online (Sandbox Code Playgroud)

不幸的是我系统地得到了

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Map'; …
Run Code Online (Sandbox Code Playgroud)

ajax spring json spring-mvc

7
推荐指数
3
解决办法
3万
查看次数

如何在 Spring Boot RestController 中映射动态查询参数

是否可以使用 Spring Boot 使用动态名称映射查询参数?我想映射如下参数:

/products?filter[name]=foo
/products?filter[length]=10
/products?filter[width]=5
Run Code Online (Sandbox Code Playgroud)

我可以做这样的事情,但这需要知道每个可能的过滤器,我希望它是动态的:

@RestController
public class ProductsController {
    @GetMapping("/products")
    public String products(
            @RequestParam(name = "filter[name]") String name,
            @RequestParam(name = "filter[length]") String length,
            @RequestParam(name = "filter[width]") String width
    ) {
        //
    }
}
Run Code Online (Sandbox Code Playgroud)

如果可能,我正在寻找允许用户定义任意数量可能的过滤器值的东西,以及那些HashMap被 Spring Boot映射为 a的东西。

@RestController
public class ProductsController {
    @GetMapping("/products")
    public String products(
            @RequestParam(name = "filter[*]") HashMap<String, String> filters
    ) {
        filters.get("name");
        filters.get("length");
        filters.get("width");
    }
}
Run Code Online (Sandbox Code Playgroud)

发布在此问题上的答案建议使用@RequestParam Map<String, String> parameters,但是这将捕获所有查询参数,而不仅仅是那些匹配的filter[*].

java query-parameters spring-boot spring-restcontroller

2
推荐指数
1
解决办法
1万
查看次数