繁体   English   中英

如何在 spring-web servlet 中接受 text/csv 作为内容类型?

[英]How to accept text/csv as content-type in spring-web servlet?

我想创建一个生成text/csv内容的简单网络服务。 但我不能要求它:

@RestController
public class MyServlet {
    @PostMapping(produces = {"text/csv", "application/json"})
    public Object post() {
        //...
    }
}

spring.mvc.contentnegotiation.media-types.csv=text/csv

当我使用Content-Type: text/csv作为 http 标头发送 post 请求时,出现以下错误:

415: Content type 'text/csv' not supported

这是我的配置:

@Configuration
public class ContentNegotiationConfiguration implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer
                .favorParameter(true) //favor &format=csv
                .defaultContentType(MediaType.APPLICATION_JSON)
                .parameterName(format);
                //.mediaType("csv", new MediaType("text", "csv")) //also tried without success
    }
}

如果您(客户端)期望csv内容,客户端应将text/csv作为Accept标头而不是Content-Type传递。

使用 JavaSpring + Swagger:

@PostMapping(value = "/yourPathValue", consumes = MULTIPART_FORM_DATA) // MULTIPART_FORM_DATA = "multipart/form-data"
public void yourMethodName(@RequestParam("csv") final MultipartFile csv) {
   // TODO your awesome logic
}

暂无
暂无

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

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