Reputation: 2281
In my java code, I am trying to access a microservice using RestTemplate
as the following:
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.
Upvotes: 0
Views: 3657
Reputation: 2281
It turned out that the value null of some document ids causes the problem. Perhaps, because @RequestBody does not accept a null value.
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();
}
Upvotes: 1
Reputation: 26
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();
}
Upvotes: 0