Solve the problem of SpringCloud Feign transfer object parameter call failure
- 2021-10-11 18:23:19
- OfStack
SpringCloud Feign Transfer Object Parameter Call Failed
GET request mode is not supported Replace Feign native httpclient with Apache HttpClient @ RequestBody receives json parametersbootstrap-local.yml
feign:
httpclient:
enabled: true
pom.xml
<!-- Use Apache HttpClient Replace Feign Native httpclient -->
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>8.18.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
feignClient:
@FeignClient(name = "hd-ucenter-server", fallback = SysTestServerFallbackImpl.class)
public interface SysTestServer {
@RequestMapping(value = "/test/test", method = RequestMethod.POST, consumes = "application/json")
Object test(CurrentUser currentUser);
}
RestController:
@RestController
@PostMapping("/test")
public class TestController {
@RequestMapping(value = "/test")
public Object test(@RequestBody CurrentUser currentUser) {
System.out.printf(" Call test\n");
return currentUser;
}
}
The problem that Feign exception cannot be passed in SpringCloud
Because the exception thrown inside cloud is not handled, Feign gets the default wrapper exception result of spring as follows:
{
"timestamp": "2017-12-27 15:01:53",
"status": 500,
"error": "Internal Server Error",
"exception": "com.keruyun.loyalty.commons.master.exception.BusinessException",
"message": "Request processing failed; nested exception is {\"code\":1000, \"message\":\"test Exception\"}",
"path": "/coupon/cloud/commercial/8469"
}
Return status under custom exception handling
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandlerResolver {
// Internal service exception handling
@ExceptionHandler(InternalApiException.class)
public ResultResp<?> handleGlobalException(HttpServletResponse response, InternalApiException internalApiException) {
ResultResp<?> resultResp = internalApiException.getResultResp();
log.error(internalApiException.getMessage(), internalApiException);
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());// Return 500 Anomaly
response.setContentType(MediaType.APPLICATION_JSON_UTF8.toString());
return resultResp;
}
}