[英]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.