简体   繁体   English

如何修复错误org.springframework.web.client.HttpClientErrorException: 400 null,当使用RestTemplate访问微服务时

[英]How to fix the error org.springframework.web.client.HttpClientErrorException: 400 null, when using RestTemplate to access Microservice

In my java code, I am trying to access a microservice using RestTemplate as the following:在我的 java 代码中,我尝试使用RestTemplate访问微服务,如下所示:

    headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    entity = new HttpEntity<BigDecimal>(c.getDocumentId(),headers);                     
    ResponseEntity<String> response =    restTemplate.exchange("http://localhost:8082/document-external-id", HttpMethod.POST, entity, String.class);
    String externalId= response.getBody();

On the server side, I have this:在服务器端,我有这个:

@RequestMapping(value="/document-external-id", method=RequestMethod.POST)

String getExternalDocumentId(@RequestBody  BigDecimal documentId)
{
    System.out.println("Documents External Id Microservice called.....Params are:"+documentId);

    Documents document =documentsService.findDocumentById(documentId);//invoke the method to get the document

    System.out.println("Documents External Id microservice is DONE!.......................................Returning value:"+document.getExternalDocumentId());

    return document.getExternalDocumentId();

}

When I run my code, here's the output with the error on the client side, which occurs at this line:当我运行我的代码时,这是客户端出现错误的输出,发生在这一行:

ResponseEntity<String> response =    restTemplate.exchange("http://localhost:8082/document-external-id", HttpMethod.POST, entity, String.class);

The error is:错误是:

org.springframework.web.client.HttpClientErrorException: 400 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:85)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:661)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
    at ae.gov.adm.saeed.web.controller.util.CircularsControllerUtil.circularListView(CircularsControllerUtil.java:151)
    at ae.gov.adm.saeed.web.controller.CircularsController.viewCircularList(CircularsController.java:58)
    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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:335)
    at ae.gov.adm.saeed.web.security.AuthFilter.doFilter(AuthFilter.java:610)
    at ae.gov.adm.common.web.filter.AbstractFilter.doFilter(AbstractFilter.java:47)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
400 null                    

On the server side, I have the following output with a Warning message:在服务器端,我有以下带有警告消息的输出:

Documents External Id Microservice called.....Params are:9191
2020-02-06 12:18:19.086  WARN 1752 --- [nio-8082-exec-5] org.hibernate.orm.deprecation            : HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead
Hibernate: select this_.id as id1_0_0_, this_.doc_master_id as doc_master_id2_0_0_, this_.entry_id as entry_id3_0_0_, this_.external_doc_id as external_doc_id4_0_0_, this_.file_path as file_path5_0_0_, this_.name as name6_0_0_, this_.server_address as server_address7_0_0_, this_.type as type8_0_0_, this_.upload_date as upload_date9_0_0_ from mu_documents this_ where this_.id=?
Documents External Id microservice is DONE!.......................................Returning value:{A66CD1F8-839F-432F-9C9C-4EAE961A8F46}
2020-02-06 12:18:19.135  WARN 1752 --- [nio-8082-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: java.lang.String ae.gov.adm.DocumentsMicroservice.getExternalDocumentId(java.math.BigDecimal)]

How to fix the error?如何修复错误? Thanks.谢谢。

It turned out that the value null of some document ids causes the problem.事实证明,某些文档 id 的值 null 导致了问题。 Perhaps, because @RequestBody does not accept a null value.也许是因为@RequestBody 不接受空值。

Here's the update to fix the problem:这是解决问题的更新:

  if(documentId!=null)
                        {
                            headers = new HttpHeaders();
                            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
                            headers.setContentType(MediaType.APPLICATION_JSON);
                            entity = new HttpEntity<BigDecimal>(documentId,headers);                        
                            ResponseEntity<String> response =    restTemplate.exchange("http://localhost:8082/document-name", HttpMethod.POST, entity, String.class);
                            documentName= response.getBody();
                        }

Have you tried with an annotation like:您是否尝试过使用以下注释:

  @PostMapping(value = "/document-external-id", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public String getExternalDocumentId(@RequestBody  BigDecimal documentId){

       System.out.println("Documents External Id Microservice called.....Params are:"+documentId);

       Documents document =documentsService.findDocumentById(documentId);//invoke the method to get the document

       System.out.println("Documents External Id microservice is DONE!.......................................Returning value:"+document.getExternalDocumentId());

       return document.getExternalDocumentId();
}

暂无
暂无

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

相关问题 org.springframework.web.client.HttpClientErrorException:RestTemplate 中的 400 错误请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request in RestTemplate org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity - org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity org.springframework.web.client.HttpClientErrorException:415 null(Spring Resttemplate) - org.springframework.web.client.HttpClientErrorException: 415 null(Spring Resttemplate) RestTemplate.postForObject - 错误:org.springframework.web.client.HttpClientErrorException:400错误请求 - RestTemplate.postForObject - Error: org.springframework.web.client.HttpClientErrorException: 400 Bad Request org.springframework.web.client.HttpClientErrorException:400使用HttpEntity restTemplate.exchange的错误请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request using HttpEntity restTemplate.exchange org.springframework.web.client.HttpClientErrorException:400空 - org.springframework.web.client.HttpClientErrorException: 400 null org.springframework.web.client.HttpClientErrorException:400错误的请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request org.springframework.web.client.HttpClientErrorException$BadRequest: 400 错误请求 - org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request org.springframework.web.client.HttpClientErrorException: 400 错误的 PUT 请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request for PUT org.springframework.web.client.HttpClientErrorException:401空 - org.springframework.web.client.HttpClientErrorException: 401 null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM