简体   繁体   English

将内容发送到Jersey Rest端点时出现错误415(不受支持的媒体类型)

[英]Error 415 (Unsupported Media Type) when sending content to Jersey Rest endpoint

I have a Java Jersey class on the backend of my project which has the goal to consume a MULTIPART_FORM_DATA sent from an ajax frontend. 我在项目的后端有一个Java Jersey类,其目标是使用从ajax前端发送的MULTIPART_FORM_DATA。 When I send the data, I receive a 415 (Unsupported Media Type), even though the content type is formatted as: 当我发送数据时,即使内容类型的格式为:我也会收到415(不受支持的媒体类型)。

content-type: multipart/form-data; 内容类型:multipart / form-data;

My backend method is configured as follow: 我的后端方法配置如下:

@POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Path("/fileupload") enter code here public Response uploadFile( @FormDataParam("file") FormDataContentDisposition cdh,@FormDataParam("file") InputStream fileStream) { System.out.println("hello there"); Payment newPayment = new Payment(); return Response.ok().entity(newPayment).build(); } @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Path("/fileupload")在此处输入代码public Response uploadFile( @FormDataParam("file") FormDataContentDisposition cdh,@FormDataParam("file") InputStream fileStream) { System.out.println("hello there"); Payment newPayment = new Payment(); return Response.ok().entity(newPayment).build(); } public Response uploadFile( @FormDataParam("file") FormDataContentDisposition cdh,@FormDataParam("file") InputStream fileStream) { System.out.println("hello there"); Payment newPayment = new Payment(); return Response.ok().entity(newPayment).build(); }

Ajax code is configured as follow: $.ajax({ url: '/path/api/fileupload', type: "POST", data: formData, processData: false, contentType: false, success: function(response) { alert("hello there") }, error: function(jqXHR, textStatus, errorMessage) { alert(errorMessage); // Optional } }); Ajax代码配置如下: $.ajax({ url: '/path/api/fileupload', type: "POST", data: formData, processData: false, contentType: false, success: function(response) { alert("hello there") }, error: function(jqXHR, textStatus, errorMessage) { alert(errorMessage); // Optional } });

What am I doing wrong? 我究竟做错了什么?

You have contentType set to false, it may be that the REST service is expecting MULTIPART_FORM_DATA and you're not sending that explicit contentType. 您已将contentType设置为false,可能是REST服务期望MULTIPART_FORM_DATA而您没有发送该明确的contentType。

@Consumes(MediaType.MULTIPART_FORM_DATA) means you need to pass an explicit contentType of "multipart/form-data" , which you are not doing, you are passing "false" . @Consumes(MediaType.MULTIPART_FORM_DATA)意味着您需要传递一个显式的contentType "multipart/form-data" ,这不是您要做的,而是传递了"false"

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

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