MultipartException and IOFileUploadException when requests receives MultipartFile - java

I'm developing a spring API and I have an endpoint that receives a MultipartFile as parameter but in the production environment I'm receiving the following error in some cases:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.impl.IOFileUploadException: Processing of multipart/form-data request failed. java.io.EOFException
I'm using spring-boot-starter-tomcat:2.4.7 that uses tomcat:9.0.46
I'm having some difficulty to debug this error because it throws before the code starts so it never reaches the logs that I put.
Thank you.
Regards.

If this does not happen all the time and only sometimes, its probably because the upload got interrupted ( browser got closed for example ). Therefore, there is nothing for you to change in the code.
You can get more details if you enable Spring MVC debug logging
application.properties
logging.level.org.springframework.web: DEBUG
Here's additional info about how you can efficiently log these kinds of errors.

Related

ResourceAccessException: I/O error on POST request for .. read timed out ERROR

When I call the rest end point (Rest Template http client) "myservice..com/rest/api/" i get the below error sometime .What can be the cause of this error? And is this client or server error?
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "myservice..com/rest/api/": read timed out;
exception is java.net.SocketTimeoutException: read timed out
Well, the exception is declarative in nature. That exception signals that a timeout has occurred on a socket read or accept, as per Java API describes. In shorter words, there is a network connectivity issue that has nothing to do with your code.
A couple of questions that you may want to figure out first:
Is the service myservice..com/rest/api/ up? Is it visible from you computer/host?
Can you hit it an status endpoint to check if it is alive?
Can you make a different HTTP request to the API GET, DELETE?
What happens if you query the endpoint with cURL or Postman?

How to deal with "feign.FeignException: [301 Moved Permanently] during [GET]"?

i'm trying to do a request with FeignClient in my Spring-Boot app to https://pixabay.com/api?key=17079440-d2facf535011df5d60e5ccfe2&q=Playstation%203&image_type=photo but but this exception has been throw:
2020-06-17 21:10:24.730 ERROR 14008 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException: [301 Moved Permanently] during [GET] to [https://pixabay.com/api?key=17079440-d2facf535011df5d60e5ccfe2&q=Playstation%203&image_type=photo] [PixabayClient#getImageUrl(String,String,String)]
https://pastebin.com/yeNbcP5k
I already tried to make a request through the postman and the browser, and the response was returned normally :(
Here is my FeignClient class: https://pastebin.com/MFhFs5um
And there is my pom.xml: https://pastebin.com/4XXjYkyE
Probably this is something your application can resolve.
What your browser does is to automatically follow the redirect and execute another request to the URL given in the redirection response.
Now you could catch the exception, take the URL and run another request just like the browser does.
This often happens when you confuse HTTP and HTTPS in your request.

Tomcat: Attachment errors while using https

I've configured tomcat (8.5.16) to use https (using org.apache.coyote.http11.Http11NioProtocol), the application (based on spring framework) works fine (like before, with http) but frequently I have an error when the user upload an attachment. It happens randomly, sometime I have this error:
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Channel is in closing state
and sometimes this:
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:297)
Before, using http there were no errors...
Any idea?
Cheers
Matteo
It was a problem with http/2, not https.
Removing this
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
from connector, it works as well as before.
Matteo

What is expected response when server throws application exception like IllegalArgrumentException?

When I make a rest request and server throws an application exception like IllegalArgumentException, I get response with http status 404.
Wouldn't a response with http status 500 be better?
Or what would actually be the expected response when an application exception is thrown? Is there some default behaviour in resteasy, spring or tomcat itself?
I know I can use an ExceptionMapper for resteasy, but is this really best practice or is there a better alternative?
I'm using following setup:
spring
resteasy
tomcat 7
Update:
The problem I'm facing is that I have 2 web applications, one is returning http status 500 and the other is returning http status 400 when an IllegalArgumentException is thrown. I can't figure out why they behave different. It seems to me, that both web application have the same spring and resteasy configuration.
When I'm debugging, I see that resteasy is transforming the IllegalArgumentException to a org.jboss.resteasy.spi.UnhandledException, but the response and the response status code, respectively, is not touched.
So besides not knowing which http status code would be the expected one (400, as Jon Skeet and Stefano Cazzola already pointed out), I didn't know either why the 2 web applications behaves different.
I couldn't find any resteasy ExceptionMapper in both web application.
So is there some default behavior in resteasy, spring or tomcat, which is mapping an IllegalArgumentException to http status 404 or http status 500? Or how can this happen?
The response code is correlated to the error happened in the server. If the error is related to an invalid input received from the client, then the formally correct response status is 400 (Bad Request). The difference with 500 is that returning 400 is the correct behaviour for the server: means, the server processed correctly and responded with an error because the request was wrong. If the same request will ever be resubmitted, it will receive the same error response. The server is not supposed to correct this error, it is up to the client.
You can take this link as a reference

Java Web Service error: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog

I have a problem creating and connecting a Java client to a running Web Service.
I use the following code:
Service myService = null;
URL wsdlLocation = new URL("http://myservice?wsdl");
QName serviceName = new QName(wsdlLocation, "MyService");
Service myService = new Service(wsdlLocation, serviceName);
where the Service class was created with the following command:
wsimport -d gen -keep http://myservice?wsdl
I've also tried with a client generated by Apache cxf 2.4's wsdl2java, but got the same result.
(I've changed the WSDL location and Service class name only for this post, in the code I use the original ones.)
But I get an exception when I call the Web Service deployed on the appserver, when creating the service with the new Service() command.
But: I've tested the wsdl location with SOAP UI and it works perfectly.
Also, I've created a Mock Service using Soap UI and my Java client could connect to it, call it and get the results back.
The problem appears when I want' to call the web service running on the appserver.
Stacktrace:
javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:149)
at org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:98)
at javax.xml.ws.Service.<init>(Service.java:76)
at MyService.<init>(MyService.java:42)
at mypackage.createService(AClass.java:288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:100)
at org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:199)
at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:147)
... 12 more
Caused by: javax.wsdl.WSDLException: WSDLException: faultCode=PARSER_ERROR: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col,system-id]: [1,0,"http://myservice?wsdl"]
at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:256)
at org.apache.cxf.wsdl11.WSDLManagerImpl.getDefinition(WSDLManagerImpl.java:205)
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:98)
... 14 more
Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col,system-id]: [1,0,"http://myservice?wsdl"]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:677)
at com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2139)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2045)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1134)
at org.apache.cxf.staxutils.StaxUtils.readDocElements(StaxUtils.java:1248)
at org.apache.cxf.staxutils.StaxUtils.readDocElements(StaxUtils.java:1142)
at org.apache.cxf.staxutils.StaxUtils.read(StaxUtils.java:1069)
at org.apache.cxf.wsdl11.WSDLManagerImpl.loadDefinition(WSDLManagerImpl.java:247)
... 16 more
Could someone please help me?
I encountered this error and found that it appeared to be due to using a URL that returned an HTTP 302 redirect instead of the WSDL directly.
The URL I was using was in the format /Service?wsdl, which redirected to a URL in the format /Service/wsdl/Service.wsdl. Once I used the redirect target URL directly, everything worked.
I remember reading that it could be related to the endpoint expecting a trailing '/'. I'm not sure if this valid, but please try it and post here it if works.
I had a similar error and when I checked the server logs - it was related to http server having encountered an unsupported http method in request. Due to this teh server returns an HTTP response that the SOAP client cannot handle ... hence Unexpected EOF in prolog
Below is a snippet my web-server log for tomcat "localhost.XXXX.log"
org.apache.tomcat.util.descriptor.web.SecurityConstraint.findUncoveredHttpMethods For security constraints with URL pattern [/services/*] only the HTTP methods [POST GET] are covered. All other methods are uncovered.
This gave following error on client side
com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
In my case since I was downgrading a secured service to unsecured and my web.xml had following stray entry that was restricting GET and post with a security constraint and I wasnt sending needed security parameters in the request..
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted web services</web-resource-name>
<url-pattern>/services/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
I removed this constraint to get rid of this error :-)
In your case it may not related to security constraint - but for sure its related to server sending raw http response - Please check your server/ client configuration and ensure it sends appropriate http request that are liked by http server
I had a similar error and when I checked the server logs - it was related to http server having encountered an unsupported http method in request returns an HTTP response that the SOAP client cannot handle ... check your web-server logs
Surely this isn't due to character or format issue.
It's possible that the server didn't return any data, of which the Woodstox (wstx) was trying to parse into XML and failed, which resulted to this error.
I had similiar issue and i have resolved with debugging.
When i try to download WSDL in runtime i found that i could not get the WSDL because of the proxy.
Please check that you can access the WSDL in runtime like this code:
try {
String wsdl = org.apache.commons.io.IOUtils.toString(new URL("http://YOUR_WSDL_URL"));
System.out.println("WSDL => "+wsdl);
} catch (IOException e) {
e.printStackTrace();
}
One of the imports are missing. Please check routes to your xsd's.
I had the same problem. I had to use the full path to the WSDL file to make it works.
My contribution to this error is this:
I don't get this error when I am in debugging mode.
It seems that when the executor of the Java program that queries the WSDL Service (Java in my case) has the time to perform many requests, like in debugging mode, because of when sometimes the programs stops because I put some breakpoints, in this case I don't get this error.
When I run the program in production mode, yes.
But the requests are the same, the WSDL doesn't change, as well ad the endpoints.
It's like a limitation of the web Server, I think.

Categories