简体   繁体   English

Spring Rest:在RequestParam中传递一个对象

[英]Spring Rest : pass an object in RequestParam

I want to pass a list of objects in my request through @RequestParam but it doesn't work.我想通过@RequestParam 在我的请求中传递一个对象列表,但它不起作用。
If I do :如果我做 :

 public List<Affaire> getAllAffaires(
                 @RequestParam(value = "consultantId", required = false) Long consultantId,
                 @RequestParam(value = "filtres", required = false) List<FilterDTO> filtres)

It doesnt go into the method, I get a 500 Error.它没有进入方法,我收到 500 错误。
If I do :如果我做 :

 public List<Affaire> getAllAffaires(
                 @RequestParam(value = "consultantId", required = false) Long consultantId,
                 List<FilterDTO> filtres)

I am getting a :我得到一个:

Unable to evaluate the expression Method threw 'java.lang.IllegalArgumentException' exception.

If I pass another object which contains the list as :如果我传递另一个包含列表的对象:

 public List<Affaire> getAllAffaires(
                 @RequestParam(value = "consultantId", required = false) Long consultantId,
                 FilterDTOList filtres)

I get into the method but the property listFiltre of FilterDTOList is always null.我进入了该方法,但 FilterDTOList 的属性 listFiltre 始终为空。

In Front Side (AngularJS) the call is :在前端(AngularJS)中,调用是:

function Affaire ($resource, DateUtils) {
        var resourceUrl =  'api/affaires/:id';

        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true,
                params:{
                    consultantId:'@consultantId',
                    filtres:'@filtres'
                },

How can I do pass my List as a parameter of my request?如何将我的列表作为我的请求的参数传递?

try with array instead of List and then convert that array to List尝试使用数组而不是列表,然后将该数组转换为列表

    @PostMapping(value = "/updateAllAffaires")
    @ResponseBody
    public List<FilterDTO> getSelectedGenes(@RequestParam(value = "consultantId", required = false) Long consultantId,
                                            @RequestBody FilterDTO[] filtersDto) {
        List<FilterDTO> filters = new ArrayList<>();
        // here you can use the logic to convert array to list
        return filters;
    }

Try using @ModelAttribute or @Requestbody instead of @RequestParam.尝试使用@ModelAttribute 或@Requestbody 代替@RequestParam。

public List<Affaire> getAllAffaires(
             @RequestParam(value = "consultantId", required = false) Long consultantId,
             @ModelAttribute(value = "filtres", required = false) List<FilterDTO> filtres)

You can read this answer for better understanding.您可以阅读答案以更好地理解。 This will also help. 也会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM