简体   繁体   English

Spring RestTemplate 给出 $InternalServerError: 500 null。 在 POSTMAN 和 SMALL FILES 中同样有效

[英]Spring RestTemplate gives $InternalServerError: 500 null. Same works in POSTMAN and for SMALL FILES

I have a Spring batch application which does the below operations,我有一个执行以下操作的 Spring 批处理应用程序,

  1. Read a file from AmazonS3 bucket.从 AmazonS3 存储桶中读取文件。
  2. Write it to local写到本地
  3. Zip the file and send to Rest API as byte array (API accepts byte[])压缩文件并作为字节数组发送到 Rest API(API 接受字节[])

when the job runs for small Files (below 2 MB), it is working fine and I am getting the response from API But when it is trying to hit large files, API throws below exception当作业针对小文件(低于 2 MB)运行时,它工作正常并且我从 API 得到响应但是当它试图访问大文件时,API 抛出以下异常

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 null org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 空

Same large file if I hit through POSTMAN, I am getting proper response.如果我通过 POSTMAN 访问相同的大文件,我会得到正确的响应。

My Rest call is as below我的休息电话如下

       ClientHttpRequestFactory requestFactory = new     
              
      HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());

    RestTemplate restTemplate = new RestTemplate(requestFactory);
        URI uri = new URI(submitUrl);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
            
            body.add("inputDoc", byteArray);
            body.add("metaData", createOcrSubmitMap(fileName));
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(uri, requestEntity, String.class);

Note: I am able to write this byte[] to a file and can open and see the file correctly.注意:我可以将这个 byte[] 写入一个文件,并且可以正确打开并查看该文件。

The same set of code works fine with small files!!同一组代码适用于小文件!

What would went wrong with large files??大文件会出什么问题??

Complete stack trace完整的堆栈跟踪

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:79) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:124) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:463) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibA org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 在 org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:79) 在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:124) ) 在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) 在 org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) 在 org.springframework.web.client.RestTemplate。 handleResponse(RestTemplate.java:778) 在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) 在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) 在 org.springframework .web.client.RestTemplate.postForEntity(RestTemplate.java:463) 在 org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) 在 org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibA opProxy.java:749) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChu opProxy.java:749) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) 在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) 在 org.springframework.transaction .interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)在 org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java :688) 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java :331) 在 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) 在 org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChu nkContext(TaskletStep.java:273) at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:203) at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:399) at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:313) at org.springframework.batch.core.launch.supp nkContext(TaskletStep.java:273) 在 org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) 在 org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java: 375) 在 org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) 在 org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) 在 org.springframework.batch .core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) 在 org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:203) 在 org.springframework.batch.core.job.SimpleStepHandler .handleStep(SimpleStepHandler.java:148) 在 org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:399) 在 org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) ) 在 org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:313) 在 org.springframework.batch.core.launch.supp ort.SimpleJobLauncher$1.run(SimpleJobLauncher.java:144) at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:137) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) at org.springfr ort.SimpleJobLauncher$1.run(SimpleJobLauncher.java:144) 在 org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) 在 org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher .java:137) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java。 lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java: 198) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) 在 org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) 在 org.springfr amework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy94.run(Unknown Source) at com.test.batch.config.JobLauncherConfig.jjob1(JobLauncherConfig.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.ut amework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)在 com.sun.proxy.$Proxy94.run(未知来源)在 com.test.batch.config.JobLauncherConfig.jjob1(JobLauncherConfig.java:56) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect .DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) 在 org. springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) 在 org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors. java:511) 在 java.ut il.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) il.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPooljaolExecutor. :293) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 在 java.lang.Thread.run(Thread.爪哇:748)

Exception is thrown at:异常抛出在:

ResponseEntity<String> response = restTemplate.postForEntity(uri, requestEntity, String.class);

Issue resolved.问题解决了。 Instead of passing the byte[] as it is, we should pass it as ByteArrayResource .与其按原样传递 byte[],不如将其作为ByteArrayResource传递。

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        ByteArrayResource contentsAsResource = new ByteArrayResource(byteArray){
            @Override
            public String getFilename() {
                return fileName;
            }
        };
        body.add("inputDoc", contentsAsResource);
        body.add("metaData", createOcrSubmitMap(fileName));

暂无
暂无

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

相关问题 Spring RestTemplate:BadRequest 400,null。 在调用Get Request时 - Spring RestTemplate : BadRequest 400,null. while calling Get Request Spring restTemplate 不适用于 URL 中的单引号。 与邮递员一起工作 - Spring restTemplate not working for single quotes in URL. Works with postman REST POST 与 POSTMAN 一起正常工作,但在使用 Spring RestTemplate 时出现异常 - REST POST works correctly with POSTMAN but exception when using Spring RestTemplate Java Spring RestTemplate POST问题,而Postman工作 - Java Spring RestTemplate POST issue, while Postman works RestTemplate 交换超时但 PostMan 有效 - RestTemplate Exchange Timeouts but PostMan Works Postman和RestTemplate的API调用中的主体参数相同,但只有Postman有效 - Same body parameters in Postman and RestTemplate's API call but only Postman works Spring 集成 HttpServerErrorException$InternalServerError: 500: 超时内未收到回复 - Spring Integration HttpServerErrorException$InternalServerError: 500 : No reply received within timeout 对于邮递员中不为null的double字段,RestTemplate返回null - RestTemplate returns null for double field that is not null in postman AWS API Gateway - 在Java中使用Spring RestTemplate访问API时的连接超时,使用Postman可以正常工作 - AWS API Gateway - Connection timeout on accessing API using Spring RestTemplate in Java where it works fine using Postman Spring Boot restTemplate POST请求bad_certificate,但在SOAPUI和Postman中有效 - Spring Boot restTemplate POST request bad_certificate, but works in SOAPUI and Postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM