In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-05-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to solve the problem that Feign,@RequestBody cannot inherit in Spring Cloud". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to solve the problem of using Feign,@RequestBody in Spring Cloud.
directory
Using Feign in Spring Cloud,@RequestBody cannot inherit issues
cause analysis
solutions
Spring cloud problems with feign
1. Examples
2. First visit timeout problem
3. In the FeignClient interface, if you use @PathVariable, you must specify its value.
Using Feign in Spring Cloud,@RequestBody cannot inherit issues
According to the official website FeignClient example, write a simple updateUser interface, defined as follows
@RequestMapping("/user")public interface UserService { @RequestMapping(value = "/{userId}", method = RequestMethod.GET) UserDTO findUserById(@PathVariable("userId") Integer userId); @RequestMapping(value = "/update", method = RequestMethod.POST) boolean updateUser(@RequestBody UserDTO user);}
implementation class
@Override public boolean updateUser(UserDTO user) { LOGGER.info("===updateUser, id = " + user.getId() + " ,name= " + user.getUsername()); return false; }
Perform unit tests and find that expected input parameters are not captured
2018-09-07 15:35:38,558 [http-nio-8091-exec-5] INFO [com.springboot.user.controller.UserController] {} - ===updateUser, id = null ,name= null
cause analysis
SpringMVC uses RequestResponseBodyMethodProcessor class to parse incoming and outgoing parameters. The following method determines whether to convert the message body according to whether the parameter has the @RequestBody annotation.
@Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(RequestBody.class); } Solutions
Since MVC uses RequestResponseBodyMethodProcessor for parameter parsing, you can implement a customized Processor to modify the supportParameter determination method.
@Override public boolean supportsParameter(MethodParameter parameter) { //springcloud interface input parameter does not write @RequestBody, and is a custom type object is also parsed as JSON if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), FeignClient.class) && isCustomizedType(parameter.getParameterType())) { return true; } return super.supportsParameter(parameter); }
The decision logic here can be defined according to the actual framework, and the purpose is to determine the interface defined for Spring Cloud, and when it is a custom object, use the same content converter as @RequestBody.
After implementing the customized Processor, you also need to make the custom configuration take effect. There are two options:
Directly replace RequestResponseBodyMethodProcessor, custom RequestMappingHandlerAdapter is required under SpringBoot.
Implement the addArgumentResolvers interface in WebMvcConfigurator
The second, simpler approach is used here, where the message converter at initialization loads as needed:
public class XXXWebMvcConfig implements WebMvcConfigurer{@Override public void addArgumentResolvers(List resolvers) { StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); stringHttpMessageConverter.setWriteAcceptCharset(false); List
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.