Spring post request parameter missing for long strings - java

I have a post method in my Spring controller that takes one request parameter called wkt. Here is my method signature:
#RequestMapping(value="/getNumberOfProperties", method=RequestMethod.POST)
public String getEstimateForNumberOfProperties(#RequestParam("wkt") String wkt) {
//code
}
In my jQuery, I am calling this method like this.
$.post(webroot + "/project/getNumberOfProperties", {wkt : wktGeomFromServer})
This works great. However, I was getting this error for wkt strings that are too long.
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'wkt' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:255) ~[spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:95) ~[spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79) ~[spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157) ~[spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124) ~[spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) ~[spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690) ~[spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) ~[spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:?]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:?]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.41]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.41]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.2.1.RELEASE.jar:3.2.1.RELEASE]
So how long is too long? After doing some testing, I have discovered that if the length of the string is 1,971,324 or less everything works. Otherwise, I get the above exception. I don't know if this is something with Spring, or tomcat, etc. Also, I have verified with Chrome that wkt parameter is getting sent in all cases, regardless of length. So the Spring error message when it is too long does not make much sense to me. Is 1,971,324 some magic number somewhere?
Any suggestions?

Could it be server configuration issue? For example Tomcat has 2MB maximum post request content size by default.

Related

Xenserver Java API start VM

I am using XenServer 6.5
In Java, I use SDK library
<dependency>
<groupId>net.java.dev.vcc.thirdparty</groupId>
<artifactId>xen-api</artifactId>
<version>6.2.0-3.1</version>
</dependency>
Then I create my instance with a template.
VM template = VM.getByNameLabel(connection, templateName).iterator()
.next();
VM vm = template.createClone(connection, instanceName);
vm.setIsATemplate(connection, false);
vm.removeFromOtherConfig(connection, "disks");
vm.setNameLabel(connection, instanceName);
vm.setMemoryStaticMin(connection, new Long(256));
vm.setMemoryDynamicMin(connection, new Long(256));
vm.setMemoryDynamicMax(connection, new Long(256));
vm.setMemoryStaticMax(connection, new Long(256));
vm.setVCPUsAtStartup(connection, new Long(1));
After I create an instance, I try to start it.
VM vm = VM.getByNameLabel(connection, name).iterator().next();
vm.start(connection, false, false);
System.out.println(vm.getPowerState(connection));
But it returns
The server failed to handle your request, due to an internal error. The given message may give details useful for debugging the problem.
at com.xensource.xenapi.Types.checkResponse(Types.java:1609)
at com.xensource.xenapi.Connection.dispatch(Connection.java:395)
at com.xensource.xenapi.VM.start(VM.java:3118)
at com.apo.region.vm.XenConnectionsManager.startVM(XenConnectionsManager.java:79)
at com.apo.region.controller.VMController.list(VMController.java:36)
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:606)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
I searched on the Internet, and found that
the create function doesn't have two boolean parameters for:
vm.start(connection);
But I try different version of Maven with:
vm.start(connection, false, false);
And the error message doesn't have much information. Did I miss any steps?
I am really sorry to answer so late, but I just saw yesterday the question and I wished to test something before answer you.
About the memory issue, any setting of memory is in bytes, due this, the parameter is long (for allow huge memory configurations).You set your new VM to 512 bytes only in both memories, static and dynamic, less than Spectrum!!
With that though in mind, my supposition is your memory limits are too low because the GUEST OS has memory restriction (specially Windows). In this blog you can see the minimum/maximum memory for guest OS related to other hypervisor (however I think is equal in Xenserver).
Anyway, this snippet is working for me using the same maven dependency, and same xenserver - 6.5 - as you.
For more information about vm.start(con,false,false) you can download Xenserver 6.5 SDK and see the javadoc related to VM. The second parameter is to allow start in pause mode and the third one is to force the start operation.
try
{
Connection con = new Connection(new URL(url));
Session.loginWithPassword(con, user, pass);
String templateName = "template-test";
String instanceName = "testVM";
VM template = VM.getByNameLabel(con, templateName).iterator().next();
VM vm = template.createClone(con, instanceName);
vm.setNameLabel(con, instanceName);
//This numbers are upper than required minimum for Windows 7
//My Guest OS
vm.setMemoryStaticMin(connection, 1073741824L);
vm.setMemoryDynamicMin(connection, 4294967296L);
vm.setMemoryDynamicMax(connection, 1073741824L);
vm.setMemoryStaticMax(connection, 4294967296L);
vm.setVCPUsAtStartup(con, 1L);
vm.provision(con);
VM vm2 = VM.getByNameLabel(con, instanceName).iterator().next();
vm2.start(con, false, false);
System.out.println(vm2.getPowerState(con));
}
catch (Exception e)
{
e.printStackTrace();
}

Problems with Spring RestTemplate Post for Object with String

I've been struggling to solve this issue here, and i can't find what i am doing wrong.
I am trying to make a simple JSON Post, using Spring RestTemplate, and sending all information as String to be simple all the way around as follows:
RestTemplate restTemplate = new RestTemplate();
JSONObject issue = getJSONIssue(redmine);
try {
String response = restTemplate.postForObject(redmineProperties.getUrl()+"/issues.json?key="+redmineProperties.getKey(), issue.toJSONString(), String.class);
}catch(RestClientException e){
e.printStackTrace();
}
My JSONObject looks like:
{"issue":
{"priority_id":4,
"description":"asdasdasdasd",
"subject":"adasdad",
"project_id":4
}
}
This is the data i am trying to sendo via Post, and the server keep sending me the error:
[http-bio-8080-exec-7] WARN org.springframework.web.client.RestTemplate - POST request for "http://xxx.com.br/issues.json?key=b8143da980578fee4db0b33bd3cd3eec511797d6" resulted in 422 (Unprocessable Entity); invoking error handler
org.springframework.web.client.HttpClientErrorException: 422 Unprocessable Entity
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:589)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:547)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:331)
at br.com.cubbes.docmidia.controller.redmine.RedmineController.saveUser(RedmineController.java:101)
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:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
when i send the same data via a client like insomnia by Google Chrome, the servers resolves without a problem.
Do anyone know what i could be doing wrong here?
For anyone coming here for 422 Unprocessable Entity, as suggested in one of the comments, setting the content-type:application/json fixed the error for me.
RequestEntity<ValidationRequest> requestEntity = RequestEntity.post(validatorUri)
.contentType(MediaType.APPLICATION_JSON)
.body(validationRequest);
JsonNode validationResponse = restTemplate.exchange(requestEntity, JsonNode.class).getBody();
I ended up using a specific library to manipulate this REST calls:
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_java
Thanks guys

Unable to find a deserializer for the type

I am trying update a Salesforce Object using Salesforce SOAP API.
Salesforce Update call fails to update the object and throws following exception:
org.apache.axis2.AxisFault: Unable to find a deserializer for the type common.api.soap.wsdl.QueryResult Error Id: 322073137-21025 (-848058548)
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.sforce.soap.enterprise.SforceServiceStub.update(SforceServiceStub.java:11289)
at com.dps.api.salesforce.crud.SalesforceCRUDImpl.updateAccount(SalesforceCRUDImpl.java:139)
at com.dps.api.crm.service.CRMServiceImpl.addEditPersonalInfoObject(CRMServiceImpl.java:109)
at com.dps.api.controller.ConfiguationController.updatePersonalInfo(ConfiguationController.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:874)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:757)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:154)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
at java.lang.Thread.run(Thread.java:619)
I've spent hours of searching on internet but nothing solves this problem!
Any idea how to get through it?
Better late than never, right? I spent a few hours troubleshooting this undescriptive error today until finally found the cause.
I was getting this message when trying to update a list of Opportunity records. Turned out that this was because I was updating the same object that I got from a query, and one (or more) of the queried fields on that object were not writable. I think it was either "recordtype.name" or "amount". What I ended up doing was creating a new instance of Opportunity object for update purposes.
1) Query for the Opportunity records, including related lists, and certain non-writable fields.
2) Instead of updating the objects you get from the query in Step# 1, create a NEW List of NEW Opportunities for update purposes, and only populate the fields that you want to update.
ArrayList<Opportunity> queryOpps = apiCaller.executeQuery(queryString);
ArrayList<Opportunity> updateOpps = new ArrayList<Opportunity>();
for (Opportunity queryOpp : queryOpps) {
// some condition for update
if (queryOpp.getAmount() < queryOpp.getProjected_Revenue__c()) {
// now, instead of adding the queryOpp to the update list,
// create a new Opportunity object.
Opportunity updateOpp = new Opportunity();
updateOpp.setId(queryOpp.getId());
updateOpp.setStageName("Needs Review");
updateOpps.add(updateOpp);
}
}
apiCaller.update(updateOpps);
​
When you fetch parent and child object records using single SOQL statment having sub SOQL for child object and then you try to execute update on parent record then we get this error due to child record list present in parent record. The simple workaround is to set child record list(query result) to null inside parent object and then execute update on parent.

Spring REST API with Multipart File Upload not working for optional parameter

I'm using Spring framework, but not too familiar with it. I'm writing a REST API, specifically a POST method that handles a file upload where the file is optional.
I've tried to simplify the method down but still having a problem. Here is what I have
#RequestMapping(method = RequestMethod.POST, value="/items")
public String create(#RequestParam(value="file", required=false) MultipartFile file) {
return "Create";
}
The method works fine when I include a file in my form request, however when I don't have a file (as I want it to be optional). I have tried replacing the #RequestParam with a HttpServletRequest parameter yielding the same results.
I get a 500 error with the following stack trace:
java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:99)
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:77)
org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76)
org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1067)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:906)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.apache.catalina.connector.Request.parseParts(Request.java:2806)
org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
org.apache.catalina.connector.Request.getParameter(Request.java:1145)
org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:311)
org.apache.catalina.connector.Request.parseParts(Request.java:2737)
org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
org.apache.catalina.connector.Request.getParameter(Request.java:1145)
org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:480)
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:889)
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:854)
org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:259)
org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:283)
org.apache.catalina.connector.Request.parseParts(Request.java:2737)
org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
org.apache.catalina.connector.Request.getParameter(Request.java:1145)
org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
I found only one post in my search on the web and it talks about this potentially being a bug in Spring. I've been stuck on this problem for a while now and would great appreciate some help.
Thanks
Here is the solution I ended up with.
#RequestMapping(method = RequestMethod.POST, value = "/object")
ResponseEntity<Object> create(#RequestParam("is_file") boolean isFile, HttpServletRequest request) {
if(isFile) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");
} else{
// You can add other parameters (as long as it isn't a multipart parameter) and do other stuff here
}
}
I can see question is a bit old, but I put my answer here because uploading files topic is always alive.
I tested Spring Boot 1.3.5.RELEASE in context of this question.
I defined rest service as below and it works when file is not attached to multipart/form-data.
I tested service with RestTemplate client.
#RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> upload(#RequestPart(value = "name", required = false) String name,
#RequestPart(value = "file", required = false) MultipartFile file)
I had the same issue and just adding the required=false worked for me. Please find the sample code below,
#RequestMapping(method = RequestMethod.POST, value="/items")
public String create(#RequestParam(value ="file", required=false) MultipartFile file) {
return "Create";
}

Why is Tomcat complaining about attempting to write too much to a response header?

My service functions as a proxy server that will send GET requests using a proxy, add some info into the header, and return this to the client. But sometimes I get this error when I'm writing the extra headers:
org.apache.coyote.http11.HeadersTooLargeException: An attempt was made to write more data to the response headers than there was room available in the buffer. Increase maxHttpHeaderSize on the connector or write less data into the response headers.
at org.apache.coyote.http11.AbstractOutputBuffer.checkLengthBeforeWrite(AbstractOutputBuffer.java:546)
at org.apache.coyote.http11.AbstractOutputBuffer.write(AbstractOutputBuffer.java:510)
at org.apache.coyote.http11.AbstractOutputBuffer.write(AbstractOutputBuffer.java:427)
at org.apache.coyote.http11.AbstractOutputBuffer.sendHeader(AbstractOutputBuffer.java:393)
at org.apache.coyote.http11.AbstractHttp11Processor.prepareResponse(AbstractHttp11Processor.java:1468)
at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:764)
at org.apache.coyote.Response.action(Response.java:172)
at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:185)
at org.apache.coyote.Response.doWrite(Response.java:504)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:383)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:342)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:413)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:401)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:135)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:220)
at java.io.Writer.write(Writer.java:157)
at org.springframework.util.StreamUtils.copy(StreamUtils.java:106)
at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:106)
at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:40)
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:124)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
I find this strange because the only thing I add into the header is {"proxy_error":"false"}. Did I just happen to send a GET to a website that returned so many headers that I couldn't write another one in? Or am I somehow causing the buffer to get too big by never clearing it (I'm sending thousands of GET requests to random urls, each time I am copying over their headers).
#RequestMapping(value = {"/hi"}, method = RequestMethod.GET)
#ResponseBody
public String test(final String url, HttpServletResponse response) {
CloseableHttpResponse httpResponse = sendGetRequest(url);
response.setHeader(PROXY_ERROR_KEY, "false");
// Copy the fetched headers
for (Header header : httpResponse.getAllHeaders()) {
response.addHeader(header.getName(), header.getValue());
}
response.setStatus(httpResponse.getStatusLine().getStatusCode());
return "hello";
}
Happened to me as well, in Jersey. I set a breakpoint on Exception, waited for Eclipse to break on it, walked through the stack and parameters and found:
I had enabled Jersey tracing which outputs debugging info into the headers. It can be quite a lot of data, no surprise I got this error.

Categories