springmvc /springboot 接收List 入参

博客介绍了多种接收请求参数的方式,包括使用@ModelAttribute注解+对象接收、@RequestParam注解接收、数组接收、post方式json传送并使用@RequestBody注解等,还提及利用String接收参数后强转,最后欢迎大家分享更多方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方式:使用@ModelAttribute 注解 + 对象接收 

1. get 请求  入参为 projectIds=1,2,3

2. @RequestMapping("/analysis") 或者 @GetMapping("/analysis") 使用 @ModelAttribute 注解 

@RequestMapping("/analysis")
public JsonResult queryList (@ModelAttribute PhoneStatisticalAnalysisReq req) {
    List<PhoneStatisticalAnalysis> result = phoneStatisticalAnalysisService.queryAnalysis(req);
    return  JsonResult.ok(result);
}
3.创建一个请求对象,并提供对应的 get set方法 用于注入 参数值 
public class PhoneStatisticalAnalysisReq {

    private List<Long> projectIds;

    public List<Long> getProjectIds() {
        return projectIds;
    }

    public void setProjectIds(List<Long> projectIds) {
        this.projectIds = projectIds;
    }

}

4. 结果

 

第二种方式: 使用 @RequestParam 注解接收

1. get 请求

2.@RequestMapping("/analysis") 或者 @GetMapping("/analysis") 使用 @RequestParam("projectIds")   注解中的参数一定要和 请求地址中的参数一致

    @RequestMapping("/analysis")
    public JsonResult queryList ( @RequestParam("projectIds")  List<Long> projectIds) {
      ...
    }

    传递多个参数

 

    @RequestMapping("/analysis")
 public JsonResult queryList ( @RequestParam("projectIds")  List<Long> projectIds,
                               @RequestParam("projectId") Integer projectId) {
   ....
    }

3.结果展示

 

第三种方式:利用数组接收

1. get请求

2.  @RequestMapping("/analysis") 或者 @GetMapping("/analysis")  用数组 接收 名称和 请求参数一一对象即可

    @RequestMapping("/analysis")
    public JsonResult queryList ( Long[] projectIds, Integer projectId) {
     ....
    }

3. 结果

第四种方式:

1.post方式请求: json 传送  入参 [1,2,3] 

2. 发送请求 请求参数为 必须加入 @RequestBody  List<Long> projectIds 注解

    @PostMapping("/analysis")
    public JsonResult queryList (@RequestBody List<Long> projectIds) {
    ....
    }

3.结果

 

第五种方式: @RequestBody 加 对象 接收

1.post Json 请求

 

2.@RequestBody 加对象 

    @RequestMapping("/analysis")
    public JsonResult queryList (@RequestBody PhoneStatisticalAnalysisReq req) {
....
    }

      对象 :

   public class PhoneStatisticalAnalysisReq {

    private Long projectId;

    private List<Long> projectIds;


    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }

    public List<Long> getProjectIds() {
        return projectIds;
    }

    public void setProjectIds(List<Long> projectIds) {
        this.projectIds = projectIds;
    }

   
}

3.结果

 

第六种方式: 接收list<T>对象

1.请求方式 post json

[
    {
    "projectId":"1",
    "projectIds":[2,3]
    },
    {
    "projectId":"2",
    "projectIds":[4,5]
    }
]

 

2. @RequestBody List<PhoneStatisticalAnalysisReq> req

 @RequestMapping("/analysis")
    public JsonResult queryList (@RequestBody List<PhoneStatisticalAnalysisReq> req) {
....
    }

 

3. 结果

 

第七种方式:  利用String 接收然后参数,然后在后台强转

1. get请求

2. 关于强转成list 这里就不做过多赘述

    @RequestMapping("/analysis")
    public JsonResult queryList (String  params) {
    ....
    }

3. 结果

 

有更多方式 ,欢迎小伙伴们请多多指教 !!!

### 如何在物联网 (IoT) 项目中使用 SpringMVC、Spring 和 MyBatis 进行开发 #### 背景概述 随着物联网技术的发展,越来越多的企业开始采用现代化框架来构建高效的 IoT 平台。Spring 生态系统因其灵活性和强大的扩展能力成为许多开发者的选择。通过结合 SpringMVC、Spring 和 MyBatis,可以实现一个高效的数据处理和服务交互架构。 --- #### 技术栈简介 - **Spring**: 提供依赖注和面向切面编程的支持,简化应用程序的复杂度。 - **SpringMVC**: 是一种基于 MVC 的 Web 层框架,负责接收 HTTP 请求并返回响应数据。 - **MyBatis**: 是一款优秀的持久层框架,支持自定义 SQL 查询语句,适合复杂的数据库操作场景。 这些工具共同构成了一个完整的全栈解决方案,适用于 IoT 数据采集、存储和展示的需求[^1]。 --- #### 配置环境 为了集成上述组件,首先需要配置 Maven 或 Gradle 来引必要的依赖项: ```xml <!-- pom.xml --> <dependencies> <!-- Spring Framework Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` --- #### 实现步骤详解 ##### 1. 创建 Spring Boot 应用程序 创建一个新的 Spring Boot 工程,并启用 `@EnableWebMvc` 注解以激活 SpringMVC 功能。 ```java // Application.java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class IotApplication { public static void main(String[] args) { SpringApplication.run(IotApplication.class, args); } } ``` ##### 2. 定义 Controller 层 Controller 层主要用于接收来自客户端的请求并将业务逻辑委派给 Service 层。 ```java // DeviceController.java import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/devices") public class DeviceController { @GetMapping("/{id}") public String getDeviceById(@PathVariable Long id) { return "Fetching device with ID: " + id; // 替换为实际的服务调用 } @PostMapping("/") public String addDevice(@RequestBody Device device) { return "Added new device: " + device.getName(); // 替换为实际的服务调用 } } ``` ##### 3. 设计 Service 层 Service 层封装了核心业务逻辑,通常会与 DAO 层协作完成数据访问任务。 ```java // DeviceService.java @Service public class DeviceService { private final DeviceMapper deviceMapper; public DeviceService(DeviceMapper deviceMapper) { this.deviceMapper = deviceMapper; } public List<Device> getAllDevices() { return deviceMapper.selectAll(); } public Device getDeviceById(Long id) { return deviceMapper.selectByPrimaryKey(id); } } ``` ##### 4. 使用 MyBatis 映射数据库 编写 Mapper 接口以及对应的 XML 文件,用于执行具体的 SQL 操作。 ```java // DeviceMapper.java import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface DeviceMapper { List<Device> selectAll(); Device selectByPrimaryKey(Long id); } ``` 对应 XML 文件如下所示: ```xml <!-- src/main/resources/mapper/DeviceMapper.xml --> <mappers> <mapper namespace="com.example.mapper.DeviceMapper"> <select id="selectAll" resultType="com.example.model.Device"> SELECT * FROM devices </select> <select id="selectByPrimaryKey" parameterType="long" resultType="com.example.model.Device"> SELECT * FROM devices WHERE id = #{id} </select> </mapper> </mappers> ``` ##### 5. 配置数据库连接 编辑 `application.properties` 文件以设置数据库数。 ```properties # application.properties spring.datasource.url=jdbc:mysql://localhost:3306/iot_db?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml ``` --- #### 总结 以上展示了如何利用 SpringMVC 处理前端请求,借助 Spring 管理服务层事务,最后通过 MyBatis 执行底层数据库查询的操作流程。这种组合方式非常适合于 IoT 场景下的大规模并发需求和高吞吐量应用[^2][^3]。 ---
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇凝子潇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值