前端返回一个集合给后端,后端的controller接口的接收如何设置
时间: 2023-12-06 17:45:34 浏览: 31
在后端的controller接口中,可以使用以下方式接收前端返回的集合:
1. 使用@RequestParam注解
```java
@PostMapping("/collection")
public void handleCollection(@RequestParam List<String> collection) {
// 处理集合
}
```
2. 使用@RequestBody注解
```java
@PostMapping("/collection")
public void handleCollection(@RequestBody List<String> collection) {
// 处理集合
}
```
其中,使用@RequestParam注解时,前端需要以query参数的形式传递集合,如:
```
/collection?collection=value1&collection=value2&collection=value3
```
而使用@RequestBody注解时,前端需要以JSON格式传递集合,如:
```json
{
"collection": ["value1", "value2", "value3"]
}
```
需要注意的是,如果集合中包含复杂对象,需要在后端定义对应的POJO类,并使用@RequestBody注解接收。
相关问题
Java后端如何给前端写一个模糊查询客户数据的Controller接口。
假设你的客户数据存储在一个名为`Customer`的Java Bean中,其中包含了`id`、`name`、`phone`等属性。你可以按照以下步骤编写一个模糊查询客户数据的Controller接口:
1. 在Controller类中添加一个RequestMapping注解,指定接口的URL和HTTP请求方法。
```java
@RestController
@RequestMapping("/api/customers")
public class CustomerController {
// ...
}
```
2. 添加一个GET方法,用于处理查询请求。在方法中添加一个@RequestParam注解,接收前端传递的查询关键词。
```java
@GetMapping
public List<Customer> searchCustomers(@RequestParam("keyword") String keyword) {
// ...
}
```
3. 在方法中编写模糊查询的逻辑。可以使用MyBatis-Plus提供的条件构造器,例如:
```java
QueryWrapper<Customer> wrapper = new QueryWrapper<>();
wrapper.like("name", keyword).or().like("phone", keyword);
List<Customer> customers = customerMapper.selectList(wrapper);
```
4. 将查询结果返回给前端。
```java
return customers;
```
最终的代码如下:
```java
@RestController
@RequestMapping("/api/customers")
public class CustomerController {
@Autowired
private CustomerMapper customerMapper;
@GetMapping
public List<Customer> searchCustomers(@RequestParam("keyword") String keyword) {
QueryWrapper<Customer> wrapper = new QueryWrapper<>();
wrapper.like("name", keyword).or().like("phone", keyword);
List<Customer> customers = customerMapper.selectList(wrapper);
return customers;
}
}
```
这样,在前端发起`/api/customers?keyword=xxx`的GET请求时,就会返回符合关键词`xxx`的客户数据了。
写一个导出Excel的后端Controller接口
好的,以下是一个使用Spring MVC实现的导出Excel的后端Controller接口:
```java
@RestController
@RequestMapping("/api")
public class ExcelController {
@GetMapping("/export")
public ResponseEntity<byte[]> exportExcel() throws IOException {
// 构造Excel文档
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("姓名");
row.createCell(1).setCellValue("年龄");
row.createCell(2).setCellValue("性别");
row = sheet.createRow(1);
row.createCell(0).setCellValue("张三");
row.createCell(1).setCellValue(22);
row.createCell(2).setCellValue("男");
row = sheet.createRow(2);
row.createCell(0).setCellValue("李四");
row.createCell(1).setCellValue(25);
row.createCell(2).setCellValue("女");
row = sheet.createRow(3);
row.createCell(0).setCellValue("王五");
row.createCell(1).setCellValue(30);
row.createCell(2).setCellValue("男");
// 设置响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", "data.xls");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// 将Excel文档转换为字节数组并返回
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
return new ResponseEntity<>(out.toByteArray(), headers, HttpStatus.OK);
}
}
```
这个Controller使用@RestController和@RequestMapping注解标记,表示该类处理REST请求,并且请求路径为/api。它构造了一个Excel文档,将数据写入到第一个工作表的前四行中。然后,设置响应头,将Excel文档转换为字节数组,并使用ResponseEntity返回。这里使用的是HSSFWorkbook,如果需要导出其他格式的文件,可以使用XSSFWorkbook等其他类型的Workbook。