You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sonam Samdupkhangsar <So...@imail.org> on 2015/04/16 20:34:13 UTC
jaxrs with angularjs file upload question
This topic is a little off CXF Forum but I thought I would try anyways to see if someone has come across this.
I am working on a JAXRS method to upload a file with AngularJS client. I followed the sample found here for the AngularJS code https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs. On the server side, the method is found but I run into NullPointerException for the header. I will post the stack trace at the end here.
My CXF JAXRS interface :
@Path("/images")
public interface LinkableImages {
@POST
@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public List<Link> uploadFile(@Multipart(value = "userId", type = "application/json")Long userId,
@Multipart(value = "folder", type = "application/json") String foldername,
@Multipart(value="file") Attachment attachment) throws Exception;
}
My html file upload controller with AngularJS:
<div data-ng-controller = "uploadCtrl">
<input type="file" file-model="myFile"/>
<button data-ng-click="uploadFile()">upload me</button>
</div>
The AngularJS code for the HTML file input:
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
console.log("file about to be uploaded is "+ file);
var fd = new FormData();
fd.append('file', file);
fd.append('userId', 3);
fd.append('folder', '')
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': 'undefined'}
})
.success(function(){
console.log("done post");
})
.error(function(data){
console.log("failed to post "+data);
});
}
}
}]);
app.controller('uploadCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log('file is ' + JSON.stringify(file) + ' file is '+ file);
var uploadUrl = "http://localhost:8080/note/soa/linkableImages/images/upload";
fileUpload.uploadFileToUrl(file, uploadUrl);
};
}]);
This is the stack trace in the server log:
[2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal - Request [/note/soa/linkableImages/images/upload] is not a multipart request
[2015-04-16 10:30:29,428] INFO CsrfHeaderFilter 31 doFilterInternal - setting csrf token in the cookie
[2015-04-16 10:30:29,446] DEBUG ServletController 219 invokeDestination - Service http request on thread: Thread[http-nio-8080-exec-64,5,main]
[2015-04-16 10:30:29,448] DEBUG AbstractHTTPDestination 224 invoke - Create a new message for processing
[2015-04-16 10:30:29,487] DEBUG Headers 380 copyFromRequest - Request Headers: {Accept=[application/json, text/plain, */*], accept-encoding=[gzip, deflate], accept-language=[en-us], connection=[keep-alive], Content-Length=[1113], content-type=[undefined], cookie=[JSESSIONID=9D0598F4A5473149F59C385AC83AF11A; XSRF-TOKEN=d1b24eff-67e3-42f0-8707-7b4d1cbb9f24], dnt=[1], host=[localhost:8080], origin=[http://localhost:8080], referer=[http://localhost:8080/note/user/note], user-agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5], x-xsrf-token=[d1b24eff-67e3-42f0-8707-7b4d1cbb9f24]}
[2015-04-16 10:30:29,512] DEBUG LogUtils 452 doLog - Could not determine bean name for instance of class org.apache.cxf.bus.managers.PhaseManagerImpl.
[2015-04-16 10:30:29,529] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.ServiceInvokerInterceptor@5bcb1378 to phase invoke
[2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.OutgoingChainInterceptor@716b5a5c to phase post-invoke
[2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.OneWayProcessorInterceptor@3ef98830 to phase pre-logical
[2015-04-16 10:30:29,532] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c to phase unmarshal
[2015-04-16 10:30:29,534] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88 to phase pre-stream
[2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 722 outputChainToLog - Chain org.apache.cxf.phase.PhaseInterceptorChain@5ba6c109 was created. Current flow:
pre-stream [CertConstraintsInterceptor]
unmarshal [JAXRSInInterceptor]
pre-logical [OneWayProcessorInterceptor]
invoke [ServiceInvokerInterceptor]
post-invoke [OutgoingChainInterceptor]
[2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
[2015-04-16 10:30:29,539] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
[2015-04-16 10:30:29,587] DEBUG JAXRSUtils 296 selectResourceClass - Trying to select a resource class, request path : /images/upload
[2015-04-16 10:30:29,588] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
[2015-04-16 10:30:29,593] DEBUG JAXRSUtils 405 findTargetMethod - Trying to select a resource operation on the resource class com.sonam.note.webservice.jaxrs.user.UserImages
[2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : hello, request path : /upload, method @Path : /hello, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : getFilenames, request path : /upload, method @Path : /user, HTTP Method : POST, method HTTP Method : GET, ContentType : */*, method @Consumes : */*,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,596] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : deleteFile, request path : /upload, method @Path : /user/folder/delete, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,598] DEBUG JAXRSUtils 449 findTargetMethod - Resource operation uploadFile may get selected
[2015-04-16 10:30:29,598] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : multipartObject, request path : /upload, method @Path : /object, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,599] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : createFolder, request path : /upload, method @Path : /user/folder/create, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,600] DEBUG JAXRSUtils 476 findTargetMethod - Resource operation uploadFile on the resource class com.sonam.note.webservice.jaxrs.user.UserImages has been selected
[2015-04-16 10:30:29,604] DEBUG JAXRSInInterceptor 190 processRequest - Request path is: /images/upload
[2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 191 processRequest - Request HTTP method is: POST
[2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 192 processRequest - Request contentType is: undefined
[2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 193 processRequest - Accept contentType is: application/json, text/plain, */*
[2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 195 processRequest - Found operation: uploadFile
[2015-04-16 10:30:29,609] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
[2015-04-16 10:30:29,674] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
[2015-04-16 10:30:29,678] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
[2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for {http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now
java.lang.NullPointerException
at org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java:306)
thanks for any hints or suggestions I can follow.
-Sonam
RE: jaxrs with angularjs file upload question
Posted by Sonam Samdupkhangsar <So...@imail.org>.
Hi Sergey,
The CXF version I am using is 3.0.0
Thanks
-Sonam
-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
Sent: Thursday, April 16, 2015 2:34 PM
To: users@cxf.apache.org
Subject: Re: jaxrs with angularjs file upload question
Hi
Which CXF version is it ?
Cheers, Sergey
On 16/04/15 13:34, Sonam Samdupkhangsar wrote:
> This topic is a little off CXF Forum but I thought I would try anyways to see if someone has come across this.
>
>
> I am working on a JAXRS method to upload a file with AngularJS client. I followed the sample found here for the AngularJS code https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs. On the server side, the method is found but I run into NullPointerException for the header. I will post the stack trace at the end here.
>
> My CXF JAXRS interface :
>
> @Path("/images")
> public interface LinkableImages {
>
> @POST
> @Path("/upload")
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> public List<Link> uploadFile(@Multipart(value = "userId", type = "application/json")Long userId,
> @Multipart(value = "folder", type = "application/json") String foldername,
> @Multipart(value="file") Attachment attachment) throws
> Exception; }
>
> My html file upload controller with AngularJS:
>
>
> <div data-ng-controller = "uploadCtrl">
> <input type="file" file-model="myFile"/>
> <button data-ng-click="uploadFile()">upload me</button>
> </div>
>
>
> The AngularJS code for the HTML file input:
>
> app.service('fileUpload', ['$http', function ($http) {
> this.uploadFileToUrl = function(file, uploadUrl){
> console.log("file about to be uploaded is "+ file);
> var fd = new FormData();
>
> fd.append('file', file);
> fd.append('userId', 3);
> fd.append('folder', '')
> $http.post(uploadUrl, fd, {
> transformRequest: angular.identity,
> headers: {'Content-Type': 'undefined'}
> })
> .success(function(){
> console.log("done post");
> })
> .error(function(data){
> console.log("failed to post "+data);
> });
> }
>
>
> }
>
> }]);
>
> app.controller('uploadCtrl', ['$scope', 'fileUpload', function($scope,
> fileUpload){
>
> $scope.uploadFile = function(){
> var file = $scope.myFile;
> console.log('file is ' + JSON.stringify(file) + ' file is '+ file);
> var uploadUrl = "http://localhost:8080/note/soa/linkableImages/images/upload";
> fileUpload.uploadFileToUrl(file, uploadUrl);
> };
>
> }]);
>
>
> This is the stack trace in the server log:
>
> [2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal -
> Request [/note/soa/linkableImages/images/upload] is not a multipart
> request
> [2015-04-16 10:30:29,428] INFO CsrfHeaderFilter 31 doFilterInternal -
> setting csrf token in the cookie
> [2015-04-16 10:30:29,446] DEBUG ServletController 219
> invokeDestination - Service http request on thread:
> Thread[http-nio-8080-exec-64,5,main]
> [2015-04-16 10:30:29,448] DEBUG AbstractHTTPDestination 224 invoke -
> Create a new message for processing
> [2015-04-16 10:30:29,487] DEBUG Headers 380 copyFromRequest - Request
> Headers: {Accept=[application/json, text/plain, */*],
> accept-encoding=[gzip, deflate], accept-language=[en-us],
> connection=[keep-alive], Content-Length=[1113],
> content-type=[undefined],
> cookie=[JSESSIONID=9D0598F4A5473149F59C385AC83AF11A;
> XSRF-TOKEN=d1b24eff-67e3-42f0-8707-7b4d1cbb9f24], dnt=[1],
> host=[localhost:8080], origin=[http://localhost:8080],
> referer=[http://localhost:8080/note/user/note],
> user-agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)
> AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5],
> x-xsrf-token=[d1b24eff-67e3-42f0-8707-7b4d1cbb9f24]}
> [2015-04-16 10:30:29,512] DEBUG LogUtils 452 doLog - Could not determine bean name for instance of class org.apache.cxf.bus.managers.PhaseManagerImpl.
> [2015-04-16 10:30:29,529] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.interceptor.ServiceInvokerInterceptor@5bcb1378 to phase
> invoke
> [2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.interceptor.OutgoingChainInterceptor@716b5a5c to phase
> post-invoke
> [2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.interceptor.OneWayProcessorInterceptor@3ef98830 to
> phase pre-logical
> [2015-04-16 10:30:29,532] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c to phase
> unmarshal
> [2015-04-16 10:30:29,534] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88 to
> phase pre-stream
> [2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 722 outputChainToLog - Chain org.apache.cxf.phase.PhaseInterceptorChain@5ba6c109 was created. Current flow:
> pre-stream [CertConstraintsInterceptor]
> unmarshal [JAXRSInInterceptor]
> pre-logical [OneWayProcessorInterceptor]
> invoke [ServiceInvokerInterceptor]
> post-invoke [OutgoingChainInterceptor]
>
> [2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 304 doIntercept
> - Invoking handleMessage on interceptor
> org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
> [2015-04-16 10:30:29,539] DEBUG PhaseInterceptorChain 304 doIntercept
> - Invoking handleMessage on interceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
> [2015-04-16 10:30:29,587] DEBUG JAXRSUtils 296 selectResourceClass -
> Trying to select a resource class, request path : /images/upload
> [2015-04-16 10:30:29,588] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
> [2015-04-16 10:30:29,593] DEBUG JAXRSUtils 405 findTargetMethod -
> Trying to select a resource operation on the resource class
> com.sonam.note.webservice.jaxrs.user.UserImages
> [2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : hello, request path : /upload, method @Path : /hello, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : getFilenames, request path : /upload, method @Path : /user, HTTP Method : POST, method HTTP Method : GET, ContentType : */*, method @Consumes : */*,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,596] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : deleteFile, request path : /upload, method @Path : /user/folder/delete, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,598] DEBUG JAXRSUtils 449 findTargetMethod -
> Resource operation uploadFile may get selected
> [2015-04-16 10:30:29,598] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : multipartObject, request path : /upload, method @Path : /object, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,599] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : createFolder, request path : /upload, method @Path : /user/folder/create, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,600] DEBUG JAXRSUtils 476 findTargetMethod -
> Resource operation uploadFile on the resource class
> com.sonam.note.webservice.jaxrs.user.UserImages has been selected
> [2015-04-16 10:30:29,604] DEBUG JAXRSInInterceptor 190 processRequest
> - Request path is: /images/upload
> [2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 191 processRequest
> - Request HTTP method is: POST
> [2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 192 processRequest
> - Request contentType is: undefined
> [2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 193 processRequest
> - Accept contentType is: application/json, text/plain, */*
> [2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 195 processRequest
> - Found operation: uploadFile
> [2015-04-16 10:30:29,609] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
> [2015-04-16 10:30:29,674] DEBUG PhaseInterceptorChain 477 unwind -
> Invoking handleFault on interceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
> [2015-04-16 10:30:29,678] DEBUG PhaseInterceptorChain 477 unwind -
> Invoking handleFault on interceptor
> org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
> [2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for
> {http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now java.lang.NullPointerException
> at
> org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java
> :306)
>
>
> thanks for any hints or suggestions I can follow.
>
> -Sonam
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: jaxrs with angularjs file upload question
Posted by Sergey Beryozkin <sb...@gmail.com>.
Well, it does look like to me that you use a 3rd party (non-CXF) library
to pre-process a multipart payload...Please check the related forums...
Thanks, Sergey
On 21/04/15 16:30, Sonam Samdupkhangsar wrote:
> I am uploading a file using a form to a JAXRS service using CXF 3.0.0 but now I keep getting this error "Couldn't find MIME boundary: ------WebKitFormBoundary"
>
> My upload form looks like this:
>
> <form action="{restserviceurl}" method="post" enctype="multipart/form-data">
> <input type="file" name="file" size="50"/>
> <input type="submit"/>
> </form>
>
>
> I have the following bean declared in my class that extends WebMvcConfiguereAdapter:
>
> Public class AppConfig extends WebMvcConfigurerAdapter {
> @Bean(name="filterMultipartResolver")
> public CommonsMultipartResolver filterMultipartResolver() {
> CommonsMultipartResolver commons = new CommonsMultipartResolver();
> return new CommonsMultipartResolver();
> }
> ...
>
> }
>
> My JAXRS service method looks like this:
>
> @POST
> @Path("/uploadonly")
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> public List<String> upload(@Multipart(value="file") Attachment attachment);
>
> In my class that exposes the JAXRS endpoints I have a class that extends the Application but not sure what that can do:
>
> @ApplicationPath("/")
> Public class JaxRsApiApplication extends Application{
>
> }
>
>
> Thanks
> -Sonam
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Friday, April 17, 2015 3:32 AM
> To: users@cxf.apache.org
> Subject: Re: jaxrs with angularjs file upload question
>
> Hi
>
> One relevant info is here:
>
>> [2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for
>>> {http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now java.lang.NullPointerException
>>> at
>>> org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java
>>> :306)
> https://fisheye6.atlassian.com/browse/~tag=cxf-3.0.0/cxf/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java?r=c1674905a6deea5f7281f636039b9e5382187828#to306
>
>
> I've checked the code, this is where some header is retrieved from a map of headers and this map is apparently null. I've just updated the local test to have a given multi-part have no headers at all, only some content and no NPE was reported...
>
> I suspect the multipart request is somehow mis-formatted. You have this
> reported:
>
> [2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal - Request [/note/soa/linkableImages/images/upload] is not a multipart request
>
> Perhaps it is not a multipart request after all...
>
> Try to run the request via a tcp trace utility and see what is reported.
>
> Cheers, Sergey
>
RE: jaxrs with angularjs file upload question
Posted by Sonam Samdupkhangsar <So...@imail.org>.
I am uploading a file using a form to a JAXRS service using CXF 3.0.0 but now I keep getting this error "Couldn't find MIME boundary: ------WebKitFormBoundary"
My upload form looks like this:
<form action="{restserviceurl}" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50"/>
<input type="submit"/>
</form>
I have the following bean declared in my class that extends WebMvcConfiguereAdapter:
Public class AppConfig extends WebMvcConfigurerAdapter {
@Bean(name="filterMultipartResolver")
public CommonsMultipartResolver filterMultipartResolver() {
CommonsMultipartResolver commons = new CommonsMultipartResolver();
return new CommonsMultipartResolver();
}
...
}
My JAXRS service method looks like this:
@POST
@Path("/uploadonly")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public List<String> upload(@Multipart(value="file") Attachment attachment);
In my class that exposes the JAXRS endpoints I have a class that extends the Application but not sure what that can do:
@ApplicationPath("/")
Public class JaxRsApiApplication extends Application{
}
Thanks
-Sonam
-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
Sent: Friday, April 17, 2015 3:32 AM
To: users@cxf.apache.org
Subject: Re: jaxrs with angularjs file upload question
Hi
One relevant info is here:
> [2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for
>>{http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now java.lang.NullPointerException
>> at
>>org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java
>>:306)
https://fisheye6.atlassian.com/browse/~tag=cxf-3.0.0/cxf/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java?r=c1674905a6deea5f7281f636039b9e5382187828#to306
I've checked the code, this is where some header is retrieved from a map of headers and this map is apparently null. I've just updated the local test to have a given multi-part have no headers at all, only some content and no NPE was reported...
I suspect the multipart request is somehow mis-formatted. You have this
reported:
[2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal - Request [/note/soa/linkableImages/images/upload] is not a multipart request
Perhaps it is not a multipart request after all...
Try to run the request via a tcp trace utility and see what is reported.
Cheers, Sergey
Re: jaxrs with angularjs file upload question
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
One relevant info is here:
> [2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for
>>{http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now java.lang.NullPointerException
>> at
>>org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java
>>:306)
https://fisheye6.atlassian.com/browse/~tag=cxf-3.0.0/cxf/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java?r=c1674905a6deea5f7281f636039b9e5382187828#to306
I've checked the code, this is where some header is retrieved from a map
of headers and this map is apparently null. I've just updated the local
test to have a given multi-part have no headers at all, only some
content and no NPE was reported...
I suspect the multipart request is somehow mis-formatted. You have this
reported:
[2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal -
Request [/note/soa/linkableImages/images/upload] is not a multipart
request
Perhaps it is not a multipart request after all...
Try to run the request via a tcp trace utility and see what is reported.
Cheers, Sergey
RE: jaxrs with angularjs file upload question
Posted by Sonam Samdupkhangsar <So...@imail.org>.
Hi Sergey,
The CXF version I am using is 3.0.0
Thanks
-Sonam
-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
Sent: Thursday, April 16, 2015 2:34 PM
To: users@cxf.apache.org
Subject: Re: jaxrs with angularjs file upload question
Hi
Which CXF version is it ?
Cheers, Sergey
On 16/04/15 13:34, Sonam Samdupkhangsar wrote:
> This topic is a little off CXF Forum but I thought I would try anyways to see if someone has come across this.
>
>
> I am working on a JAXRS method to upload a file with AngularJS client. I followed the sample found here for the AngularJS code https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs. On the server side, the method is found but I run into NullPointerException for the header. I will post the stack trace at the end here.
>
> My CXF JAXRS interface :
>
> @Path("/images")
> public interface LinkableImages {
>
> @POST
> @Path("/upload")
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> public List<Link> uploadFile(@Multipart(value = "userId", type = "application/json")Long userId,
> @Multipart(value = "folder", type = "application/json") String foldername,
> @Multipart(value="file") Attachment attachment) throws
> Exception; }
>
> My html file upload controller with AngularJS:
>
>
> <div data-ng-controller = "uploadCtrl">
> <input type="file" file-model="myFile"/>
> <button data-ng-click="uploadFile()">upload me</button>
> </div>
>
>
> The AngularJS code for the HTML file input:
>
> app.service('fileUpload', ['$http', function ($http) {
> this.uploadFileToUrl = function(file, uploadUrl){
> console.log("file about to be uploaded is "+ file);
> var fd = new FormData();
>
> fd.append('file', file);
> fd.append('userId', 3);
> fd.append('folder', '')
> $http.post(uploadUrl, fd, {
> transformRequest: angular.identity,
> headers: {'Content-Type': 'undefined'}
> })
> .success(function(){
> console.log("done post");
> })
> .error(function(data){
> console.log("failed to post "+data);
> });
> }
>
>
> }
>
> }]);
>
> app.controller('uploadCtrl', ['$scope', 'fileUpload', function($scope,
> fileUpload){
>
> $scope.uploadFile = function(){
> var file = $scope.myFile;
> console.log('file is ' + JSON.stringify(file) + ' file is '+ file);
> var uploadUrl = "http://localhost:8080/note/soa/linkableImages/images/upload";
> fileUpload.uploadFileToUrl(file, uploadUrl);
> };
>
> }]);
>
>
> This is the stack trace in the server log:
>
> [2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal -
> Request [/note/soa/linkableImages/images/upload] is not a multipart
> request
> [2015-04-16 10:30:29,428] INFO CsrfHeaderFilter 31 doFilterInternal -
> setting csrf token in the cookie
> [2015-04-16 10:30:29,446] DEBUG ServletController 219
> invokeDestination - Service http request on thread:
> Thread[http-nio-8080-exec-64,5,main]
> [2015-04-16 10:30:29,448] DEBUG AbstractHTTPDestination 224 invoke -
> Create a new message for processing
> [2015-04-16 10:30:29,487] DEBUG Headers 380 copyFromRequest - Request
> Headers: {Accept=[application/json, text/plain, */*],
> accept-encoding=[gzip, deflate], accept-language=[en-us],
> connection=[keep-alive], Content-Length=[1113],
> content-type=[undefined],
> cookie=[JSESSIONID=9D0598F4A5473149F59C385AC83AF11A;
> XSRF-TOKEN=d1b24eff-67e3-42f0-8707-7b4d1cbb9f24], dnt=[1],
> host=[localhost:8080], origin=[http://localhost:8080],
> referer=[http://localhost:8080/note/user/note],
> user-agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)
> AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5],
> x-xsrf-token=[d1b24eff-67e3-42f0-8707-7b4d1cbb9f24]}
> [2015-04-16 10:30:29,512] DEBUG LogUtils 452 doLog - Could not determine bean name for instance of class org.apache.cxf.bus.managers.PhaseManagerImpl.
> [2015-04-16 10:30:29,529] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.interceptor.ServiceInvokerInterceptor@5bcb1378 to phase
> invoke
> [2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.interceptor.OutgoingChainInterceptor@716b5a5c to phase
> post-invoke
> [2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.interceptor.OneWayProcessorInterceptor@3ef98830 to
> phase pre-logical
> [2015-04-16 10:30:29,532] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c to phase
> unmarshal
> [2015-04-16 10:30:29,534] DEBUG PhaseInterceptorChain 242 add - Adding
> interceptor
> org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88 to
> phase pre-stream
> [2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 722 outputChainToLog - Chain org.apache.cxf.phase.PhaseInterceptorChain@5ba6c109 was created. Current flow:
> pre-stream [CertConstraintsInterceptor]
> unmarshal [JAXRSInInterceptor]
> pre-logical [OneWayProcessorInterceptor]
> invoke [ServiceInvokerInterceptor]
> post-invoke [OutgoingChainInterceptor]
>
> [2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 304 doIntercept
> - Invoking handleMessage on interceptor
> org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
> [2015-04-16 10:30:29,539] DEBUG PhaseInterceptorChain 304 doIntercept
> - Invoking handleMessage on interceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
> [2015-04-16 10:30:29,587] DEBUG JAXRSUtils 296 selectResourceClass -
> Trying to select a resource class, request path : /images/upload
> [2015-04-16 10:30:29,588] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
> [2015-04-16 10:30:29,593] DEBUG JAXRSUtils 405 findTargetMethod -
> Trying to select a resource operation on the resource class
> com.sonam.note.webservice.jaxrs.user.UserImages
> [2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : hello, request path : /upload, method @Path : /hello, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : getFilenames, request path : /upload, method @Path : /user, HTTP Method : POST, method HTTP Method : GET, ContentType : */*, method @Consumes : */*,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,596] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : deleteFile, request path : /upload, method @Path : /user/folder/delete, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,598] DEBUG JAXRSUtils 449 findTargetMethod -
> Resource operation uploadFile may get selected
> [2015-04-16 10:30:29,598] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : multipartObject, request path : /upload, method @Path : /object, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,599] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : createFolder, request path : /upload, method @Path : /user/folder/create, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,600] DEBUG JAXRSUtils 476 findTargetMethod -
> Resource operation uploadFile on the resource class
> com.sonam.note.webservice.jaxrs.user.UserImages has been selected
> [2015-04-16 10:30:29,604] DEBUG JAXRSInInterceptor 190 processRequest
> - Request path is: /images/upload
> [2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 191 processRequest
> - Request HTTP method is: POST
> [2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 192 processRequest
> - Request contentType is: undefined
> [2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 193 processRequest
> - Accept contentType is: application/json, text/plain, */*
> [2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 195 processRequest
> - Found operation: uploadFile
> [2015-04-16 10:30:29,609] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
> [2015-04-16 10:30:29,674] DEBUG PhaseInterceptorChain 477 unwind -
> Invoking handleFault on interceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
> [2015-04-16 10:30:29,678] DEBUG PhaseInterceptorChain 477 unwind -
> Invoking handleFault on interceptor
> org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
> [2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for
> {http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now java.lang.NullPointerException
> at
> org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java
> :306)
>
>
> thanks for any hints or suggestions I can follow.
>
> -Sonam
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: jaxrs with angularjs file upload question
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
Which CXF version is it ?
Cheers, Sergey
On 16/04/15 13:34, Sonam Samdupkhangsar wrote:
> This topic is a little off CXF Forum but I thought I would try anyways to see if someone has come across this.
>
>
> I am working on a JAXRS method to upload a file with AngularJS client. I followed the sample found here for the AngularJS code https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs. On the server side, the method is found but I run into NullPointerException for the header. I will post the stack trace at the end here.
>
> My CXF JAXRS interface :
>
> @Path("/images")
> public interface LinkableImages {
>
> @POST
> @Path("/upload")
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.MULTIPART_FORM_DATA)
> public List<Link> uploadFile(@Multipart(value = "userId", type = "application/json")Long userId,
> @Multipart(value = "folder", type = "application/json") String foldername,
> @Multipart(value="file") Attachment attachment) throws Exception;
> }
>
> My html file upload controller with AngularJS:
>
>
> <div data-ng-controller = "uploadCtrl">
> <input type="file" file-model="myFile"/>
> <button data-ng-click="uploadFile()">upload me</button>
> </div>
>
>
> The AngularJS code for the HTML file input:
>
> app.service('fileUpload', ['$http', function ($http) {
> this.uploadFileToUrl = function(file, uploadUrl){
> console.log("file about to be uploaded is "+ file);
> var fd = new FormData();
>
> fd.append('file', file);
> fd.append('userId', 3);
> fd.append('folder', '')
> $http.post(uploadUrl, fd, {
> transformRequest: angular.identity,
> headers: {'Content-Type': 'undefined'}
> })
> .success(function(){
> console.log("done post");
> })
> .error(function(data){
> console.log("failed to post "+data);
> });
> }
>
>
> }
>
> }]);
>
> app.controller('uploadCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
>
> $scope.uploadFile = function(){
> var file = $scope.myFile;
> console.log('file is ' + JSON.stringify(file) + ' file is '+ file);
> var uploadUrl = "http://localhost:8080/note/soa/linkableImages/images/upload";
> fileUpload.uploadFileToUrl(file, uploadUrl);
> };
>
> }]);
>
>
> This is the stack trace in the server log:
>
> [2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal - Request [/note/soa/linkableImages/images/upload] is not a multipart request
> [2015-04-16 10:30:29,428] INFO CsrfHeaderFilter 31 doFilterInternal - setting csrf token in the cookie
> [2015-04-16 10:30:29,446] DEBUG ServletController 219 invokeDestination - Service http request on thread: Thread[http-nio-8080-exec-64,5,main]
> [2015-04-16 10:30:29,448] DEBUG AbstractHTTPDestination 224 invoke - Create a new message for processing
> [2015-04-16 10:30:29,487] DEBUG Headers 380 copyFromRequest - Request Headers: {Accept=[application/json, text/plain, */*], accept-encoding=[gzip, deflate], accept-language=[en-us], connection=[keep-alive], Content-Length=[1113], content-type=[undefined], cookie=[JSESSIONID=9D0598F4A5473149F59C385AC83AF11A; XSRF-TOKEN=d1b24eff-67e3-42f0-8707-7b4d1cbb9f24], dnt=[1], host=[localhost:8080], origin=[http://localhost:8080], referer=[http://localhost:8080/note/user/note], user-agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5], x-xsrf-token=[d1b24eff-67e3-42f0-8707-7b4d1cbb9f24]}
> [2015-04-16 10:30:29,512] DEBUG LogUtils 452 doLog - Could not determine bean name for instance of class org.apache.cxf.bus.managers.PhaseManagerImpl.
> [2015-04-16 10:30:29,529] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.ServiceInvokerInterceptor@5bcb1378 to phase invoke
> [2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.OutgoingChainInterceptor@716b5a5c to phase post-invoke
> [2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.OneWayProcessorInterceptor@3ef98830 to phase pre-logical
> [2015-04-16 10:30:29,532] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c to phase unmarshal
> [2015-04-16 10:30:29,534] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88 to phase pre-stream
> [2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 722 outputChainToLog - Chain org.apache.cxf.phase.PhaseInterceptorChain@5ba6c109 was created. Current flow:
> pre-stream [CertConstraintsInterceptor]
> unmarshal [JAXRSInInterceptor]
> pre-logical [OneWayProcessorInterceptor]
> invoke [ServiceInvokerInterceptor]
> post-invoke [OutgoingChainInterceptor]
>
> [2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
> [2015-04-16 10:30:29,539] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
> [2015-04-16 10:30:29,587] DEBUG JAXRSUtils 296 selectResourceClass - Trying to select a resource class, request path : /images/upload
> [2015-04-16 10:30:29,588] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
> [2015-04-16 10:30:29,593] DEBUG JAXRSUtils 405 findTargetMethod - Trying to select a resource operation on the resource class com.sonam.note.webservice.jaxrs.user.UserImages
> [2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : hello, request path : /upload, method @Path : /hello, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : getFilenames, request path : /upload, method @Path : /user, HTTP Method : POST, method HTTP Method : GET, ContentType : */*, method @Consumes : */*,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,596] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : deleteFile, request path : /upload, method @Path : /user/folder/delete, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,598] DEBUG JAXRSUtils 449 findTargetMethod - Resource operation uploadFile may get selected
> [2015-04-16 10:30:29,598] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : multipartObject, request path : /upload, method @Path : /object, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,599] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : createFolder, request path : /upload, method @Path : /user/folder/create, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
> [2015-04-16 10:30:29,600] DEBUG JAXRSUtils 476 findTargetMethod - Resource operation uploadFile on the resource class com.sonam.note.webservice.jaxrs.user.UserImages has been selected
> [2015-04-16 10:30:29,604] DEBUG JAXRSInInterceptor 190 processRequest - Request path is: /images/upload
> [2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 191 processRequest - Request HTTP method is: POST
> [2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 192 processRequest - Request contentType is: undefined
> [2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 193 processRequest - Accept contentType is: application/json, text/plain, */*
> [2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 195 processRequest - Found operation: uploadFile
> [2015-04-16 10:30:29,609] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
> [2015-04-16 10:30:29,674] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
> [2015-04-16 10:30:29,678] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
> [2015-04-16 10:30:29,681] WARN LogUtils 452 doLog - Interceptor for {http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now
> java.lang.NullPointerException
> at org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java:306)
>
>
> thanks for any hints or suggestions I can follow.
>
> -Sonam
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com