Rest API端点,用于使用从数据库收集的数据生成CSV文件

时间:2019-09-19 13:00:51

标签: spring-boot spring-webflux

以反应方式从数据库收集的数据列表中组成CSV文件时卡住了。

我的控制器:

@RestController
@RequestMapping("/v1/foo")
@RequiredArgsConstructor
public class FooController {

    private final FooService paymentService;

    @GetMapping(value = "/export")
    @Validated
    public Mono<ResponseEntity> export(FooQuery query) {
        return fooService.export(query) // return Flux<Foo>
                             .collectList()
                             .map(foos -> ResponseEntity.ok()
                                                        .contentType(MediaType.valueOf("text/csv"))
                                                        // Somehow I need to generate CSV format resource and pass it to body(), but no clue how to do that.
                                                        .body(foos));
    }

}

如何生成CSV格式资源以将其传递给body()?

解决方案:

@GetMapping(value = "/export", produces = {"text/csv"})
    @Validated
    public Mono<ResponseEntity> export(FooQuery query) {
        return fooService.export(query) // return Flux<Foo>
                             .collectList()
                             .map(foos -> ResponseEntity.ok()
                                                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=export.csv")
                                                        .body("ffd;fdfddf;ddddd;")); 
                                                        // Need to convert List<Foo> to correct csv string that it'll work otherwise 406 NOT_ACCEPTABLE "Could not find acceptable representation"
    }

1 个答案:

答案 0 :(得分:0)

一旦有了字符串csv 我使用HttpServletResponse对象而不是像这样的ResponseEntity:

Sub FliterWithConditionalFormatting()
Dim rng As Range

'properly defing and reference your workbook and worksheet, change as requiried
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1").CurrentRegion

'The WITH..END WITH statement allows you to shorten your code and avoid using SELECT and ACTIVATE
With rng
     .AutoFilter Field:=1, Criteria1:="Phase", Operator:=xlAnd 'filter the rng

    'set the range, to conditionally format only the visible data, skipping the header row
    With .Range(Cells(2, 1), Cells(rng.Rows.Count, 17)).SpecialCells(xlCellTypeVisible)

        With .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = xlThemeColorAccent6
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

        With .Font
            .Bold = True
        End With
    End With

    .AutoFilter 'Remove the filter
End With
End Sub