欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

feign调服务参数问题

程序员文章站 2022-07-12 23:27:21
...

遇到个问题,feign调其他服务时报了个错,如下:

Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.msmp.scrm.entity.base.CluePond
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.msmp.scrm.entity.base.CluePond
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

意思大概是说,LinkedhashMap不能强制转换成CluePond对象。为啥会有linkedHashMap对象呢?
核心代码截取:

@FeignClient(name="scrm-base",url="localhost:8080")
public interface BaseService {
    @RequestMapping(value = "/public/base/cluePond/saveClue")
    public void saveCluePond(@RequestBody Object clue);
}

@RestController
@RequestMapping(value = "/public/base")
public class PublicBaseController {
    @Autowired
    private CluePondService cluePondService;

    @RequestMapping(value = "/cluePond/saveClue")
    public void saveCluePond(@RequestBody Object buClue){
         cluePondService.save(buClue);
    }
}

@Service
public class CluePondService {
    public void save(Object buClue) {
         cluePond = (CluePond)buClue;//报错地方,强制转换报错
    }
}
会出现这个原因,主要是feign传递参数封装时的原因。
@RequestParam 指定的参数会拼接在URL之后,如: ?name=xxx&age=18
@PathVariable 指定的参数会放到一个LinkedHashMap<String, ?>中
@RequestBody 指定对象中的各属性放到一个LinkedHashMap<String,?>中

如以上buClue对象:
feign调服务参数问题

相关标签: feign