微服務中的異常 java.lang.NoSuchMethodException

Caused by: java.lang.reflect.UndeclaredThrowableException: null
	at com.xmeport.msa.service.impl.NoticeServiceImpl$$EnhancerBySpringCGLIB$$b760761e.query(<generated>) ~[classes/:na]
	at com.xmeport.msa.controller.notice.NoticeService.getPage(NoticeService.java:48) ~[classes/:na]
	at com.xmeport.msa.controller.notice.NoticeService$$FastClassBySpringCGLIB$$ad0d94dc.invoke(<generated>) ~[classes/:na]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at com.xmeport.msa.controller.notice.NoticeService$$EnhancerBySpringCGLIB$$629acc0d.getPage(<generated>) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
	at org.apache.servicecomb.swagger.engine.SwaggerProducerOperation.doInvoke(SwaggerProducerOperation.java:180) [swagger-invocation-core-1.0.0.B003.H98.jar:1.0.0.B003.H98]
	... 62 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.xmeport.msa.service.impl.NoticeServiceImpl.query()
	at java.lang.Class.getDeclaredMethod(Class.java:2130) ~[na:1.8.0_191]
	at com.xmeport.msa.common.aop.DataSourceAspect.setDataSourceData(DataSourceAspect.java:34) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:626) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:44) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:55) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.5.RELEASE.jar:5.1.5.RELEASE]
	... 73 common frames omitted
 /**
     * 獲取分頁列表
     */
    @PostMapping("/get-page")
    public @ResponseBody ApiResponses<IPage<NoticeListVO>> getPage(NoticeQueryVO noticeQueryVO) {
        PageBO<NoticeQueryBO> page = PageBO.newPage(request);
        PageBO resultPage = restTemplate.postForObject("cse://msa-service/notice/get-page",
                noticeQueryVO.convert(NoticeQueryBO.class).setPage(page), PageBO.class);
        Page<NoticeListVO> noticePage = resultPage.convert(Page.class);
        return success(noticePage);
    }

在調試微服務的項目中,當調用這個接口時,可以正常調用,但是進入到Service層(這個相當於微服務的中轉層,實現業務邏輯在這裏)時,無法調用裏面的query()方法,會報上面的錯誤。

@RestSchema(schemaId = "msa.service.NoticeService")
@RequestMapping("/notice")
public class NoticeService {
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IAttachmentService attachmentService;

    @PostMapping("/get-page")
    public @ResponseBody
    PageBO getPage(@RequestBody NoticeQueryBO noticeQueryBO) {
        LambdaQueryWrapperChain<Notice> condition = noticeService.query()
                .eq((noticeQueryBO.getCategory() != null), Notice::getCategory, noticeQueryBO.getCategory())
                .eq(StringUtils.isNotBlank(noticeQueryBO.getTitle()), Notice::getTitle, noticeQueryBO.getTitle())
                .gt(noticeQueryBO.getPublishTimeStart() != null, Notice::getPublishTime, noticeQueryBO.getPublishTimeStart())
                .lt(noticeQueryBO.getPublishTimeEnd() != null, Notice::getPublishTime, noticeQueryBO.getPublishTimeEnd())
                .eq(Notice::getIsDel, "N");
        condition.orderByDesc(Notice::getPublishTime);
        IPage<Notice> page = condition.page(noticeQueryBO.getPage().convert(Page.class));
        IPage<NoticeListBO> pageBO = page.convert(e -> e.convert(NoticeListBO.class));
        return PageBO.newPage(pageBO);
    }

一開始認爲傳參有錯誤,其實這裏把查詢的方法寫對應的Serviceimpl裏,纔不會報錯。

@RestSchema(schemaId = "msa.service.NoticeService")
@RequestMapping("/notice")
public class NoticeService {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IAttachmentService attachmentService;

    @PostMapping("/get-page")
    public @ResponseBody
    PageBO getPage(@RequestBody NoticeQueryBO noticeQueryBO) {
        System.out.println(1);
        System.out.println(2);
        IPage<Notice> page = noticeService.getPage(noticeQueryBO);
        IPage<NoticeListBO> pageBO = page.convert(e -> e.convert(NoticeListBO.class));
        return PageBO.newPage(pageBO);
    }

上面的情況是針對

restTemplate.postForObject才需要的。

如果情況是

restTemplate.getForObject 可以不用,直接在service(業務中轉層)上用注入的Service的query()方法。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章