我正在使用球衣(JAX-RS)和球衣版本2.25.1的彈簧引導,並且我正在嘗試使用POST請求來上傳1個文件。球衣,彈簧引導,上傳圖片不支持的媒體類型
我已經檢查了每一個stackoverflow上的鏈接,幾乎所有的人都告訴我註冊MultipartFeature並添加init-params。我已做了所有的變化,但我仍然得到同樣的錯誤:
@Component
@ApplicationPath("/secure")
public class JerseyInitialization extends ResourceConfig {
public JerseyInitialization() {
this.register(new JacksonJsonProvider(ObjectMapperFactory.create()));
this.register(MultiPartFeature.class);
this.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
this.property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
this.packages(true, "com.jersey.resources");
}
}
我的職務要求上傳圖片是:
@Path("/images")
@Consumes(MediaType.APPLICATION_JSON)
@Component
@Transactional
public class ImageResource {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Image uploadFile(@Valid Image image,
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {
image.setData(IOUtils.toByteArray(uploadedInputStream));
image.setFilename(fileDetail.getFileName());
System.out.println("File uploaded Successfully");
return imageDao.save(image);
}
}
我的應用等級如下表所示:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/resources");
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
registration.addInitParameter("javax.ws.rs.Application", "com.verico.multipart.app.MultiPartApp");
return registration;
}
}
時我用以下命令運行卷曲:
curl -X POST -H "Authorization: Bearer db4719e2-dd76-4977-9dab-e44670213c63" -F "[email protected]" -F "name=kreditech.jpg" localhost:8080/api/secure/images/upload
我得到的錯誤如下:
"status":415,"error":"Unsupported Media Type","message":"Unsupported Media Type"