我有一个Web应用程序,在前端使用Angularjs,后端使用Resteasy + Jackson。我将一个文件从Angular组件发送到REST方法,接收方法如下所示:
@POST
@Path("/upload/attachment/for/{eventId}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(MultipartFormDataInput input,
final @PathParam("eventId") Long eventId,
@Context HttpServletRequest request) {
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
...my awesome stuff...
return Response.ok().build();
}
请求在发送时有以下标题:
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 347085
Content-Type multipart/form-data; boundary=---------------------------12164806981346771846716776342
Cookie JSESSIONID=aoBd1hgzR3GM8bSG5P-9g-vQ; csrftoken=ziQ7kN7TlMehR2aURDrmaMLYAroMsSpu
Host localhost:9000
Referer http://localhost:9000/local/myapp/index.html
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:32.0) Gecko/20100101 Firefox/32.0
问题是,此请求总是application/json
作为内容类型,而不是标题中的multipart/form-data
。我得到了:
20:12:00,490 WARN [org.jboss.resteasy.core.SynchronousDispatcher] (http-/127.0.0.1:8080-2) Failed executing POST /events/upload/attachment/for/null: org.jboss.resteasy.spi.UnsupportedMediaTypeException: Cannot consume content type
已经在HttpServletDispatcher
中内容有误。
如果这是将Content-Type设置为错误值或其他东西的JBOSS,我无法得到。
答案 0 :(得分:0)
好的,就是我在angular.js上运行了angular和server之间的额外层,它只是将请求转发到服务器。我不得不添加以下内容:
var r = null;
r = request.put({uri: url, json: inReq.body, headers: inReq.headers, form: inReq.form});
inReq.pipe(r).pipe(inRes);
这将设置来自传出请求的所有标头并对请求进行隧道传输
现在我对了content-type