FebGrommash 2023-11-12 13:34 采纳率: 50%
浏览 25

springboot中Get请求怎么接受复杂数据类型?

get请求怎么接受Dto中多个携带范型的集合参数呢?
如paramDto中有一个List参数(Dto中还有其他数据类型参数,或其他集合),这种参数如何接收传参呢?

  • 写回答

2条回答 默认 最新

  • 玥轩_521 优质创作者: 信息安全技术领域 2023-11-12 13:59
    关注

    在Java中,可以使用@RequestBody注解来接收复杂数据类型。对于DTO中的多个携带范型的集合参数,可以在DTO类中使用泛型来定义集合类型。以下是一个示例:

    首先,创建一个DTO类,例如ParamDto

    public class ParamDto {
        private List<String> stringList;
        private Map<String, Integer> stringIntegerMap;
    
        // getter和setter方法
    }
    

    然后,在Controller类中,使用@RequestBody注解来接收ParamDto对象:

    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
    
        @PostMapping("/process")
        public String process(@RequestBody ParamDto paramDto) {
            // 处理paramDto中的数据
            return "success";
        }
    }
    

    这样,当客户端发送一个包含stringListstringIntegerMap的POST请求到/process时,Spring会自动将请求体中的JSON数据反序列化为ParamDto对象。

    评论

报告相同问题?

问题事件

  • 修改了问题 11月13日
  • 创建了问题 11月12日