org.springframework.aop.AopInvocationException:来自通知的空返回值不匹配原始返回类型:公共抽象字符

Day*_*aya 4 java jpa

我有一个用列 char 定义的表,我定义了一个查询来返回该列。现在,当 db 表中的数据对于特定条件为空时,它给了我错误:

org.springframework.aop.AopInvocationException: Null return value from 
advice does not match primitive return type for: public abstract char
Run Code Online (Sandbox Code Playgroud)

下面是代码片段:

    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;

    import com.marriott.sup.beans.StepWorkflow;

    public interface flowRepo extends JpaRepository<StepWorkflow, Long> {

        @Query(
                value = "SELECT s.status FROM flow s WHERE s.XREFID 
     = :xrefId order by "
                + "s.Datestarted desc limit 1",
                nativeQuery = true)
        public char findLatestSts(@Param("xrefId") Long xrefId);

    }

    public class flowService {

        public static char INPROGRESS = 'P';
        public static char SUCCESS = 'S';
        public static char ERROR = 'E';
        public static char NOTSTARTED = 'N';

        @Autowired
        private flowRepo Fl;

        /*some basic code in between and then i am trying to call the 
      function 
      defined in repo and assign it to a character variable
    */

    Sts = Fl.findLatestSts(xrefId);
    }
Run Code Online (Sandbox Code Playgroud)

我需要它不会抛出错误,而是在为 null 的情况下分配一些默认值。需要帮助来解决有关如何处理此问题的问题。

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract char org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226) at com.sun.proxy.$Proxy108.findLatestSts(Unknown Source) at com.marriott.sup.service.StepWorkflowService.getserverStepstatus(StepWorkflowService.java:79) at com.marriott.sup.web.SampleRestController.getServerStepStatus(SampleRestController.java:91) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
Run Code Online (Sandbox Code Playgroud)

Dre*_*red 10

尝试使用Character而不是char. 当您的查询返回 NULL 并且您知道 Java 中的原始类型无法在其中保存 Null 时,会出现这种情况。因此,请使用该类型的 Object,例如int -> Integer, float -> Float, char -> Character.

或者,如果您确实需要使用原语,请将您的方法放在 try-catch 块中,以处理该问题。