获取requestmethod.get函数上的httpmediatypenotacceptableexception

llmtgqce  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(274)

我在示例项目中使用的是Spring4.3版本。其中一个函数返回一个模型userdetails。下面是控制器功能。
用户控制器.java

@RequestMapping(value="/userDetails/{name}/{userName}", method = RequestMethod.GET)
    public @ResponseBody List<UserDetailsVO> getUserDetailsByname(@PathVariable String name, @PathVariable String userName) { // line number 2222222
        List<UserDetailsVO> userDetailsVOs = null;  
        try{
            userDetailsVOs = UserDetailsDAO.getUserDetailsBynameDAO(name, userName);
        }
        catch(Exception e){
            log.error("Exception occured")
        }
        return UserDetailsVOs; // line number 1111111
    }

userdetailsdao.java文件

@Transactional(propagation=Propagation.REQUIRED)
    @SuppressWarnings("unchecked")
    @Override
    public List<UserDetailsVO> getUserDetailsBynameDAO(String name, String userName) {      
        List<UserDetailsVO> recordList = new ArrayList<UserDetailsVO>();    
        String searchValue="%"+userName+"%";
        try {   
                recordList = UserDetailsUtil.getUserRecords(getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery("from UserDetails spr where UPPER("+name+") like :searchValue").setString("searchValue", searchValue.toUpperCase()).list());         
        } catch (Exception e) {
            log.error("Exception occured")
        }

        return recordList;
    }

在上面的控制器中,如果我将用户名中的搜索字符串作为“dummy.xyz”传递,那么它将该字符串作为“dummy”,并从字符串中删除“.xyz”,这是正常的,因为路径变量删除了“.”字符,并且在调试db query之后,它返回用户名中有“dummy”的对象,这是预期的行为,但是在执行第1111111行之后,它给出了以下错误。
错误日志

[2021-03-23][18:29:42][WARN]Failed to invoke @ExceptionHandler method: public com.test.project.service.exceptions.Errors com.test.project.service.UserController.handleException(java.lang.Exception,javax.servlet.http.HttpServletResponse)
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:200)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
    at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:385)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:59)
    at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:132)
    at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1222)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1629)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
[2021-03-23][18:29:42][WARN]Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

如果我把行号2222222改成

@RequestMapping(value="/userDetails/{name}", method = RequestMethod.GET)
public @ResponseBody List<UserDetailsVO> getUserDetailsByname(@PathVariable String name, @RequestParam(value = "value", required=false) String value) {

那它就完全正常了。
我不确定@pathvariable和@requestparam在更改响应方面有什么不同。是的@requestparam确实允许搜索条目为full“dummy.xyz”,而@pathvariable只允许“dummy”,但为什么@pathvariable在db返回正确结果时仍给出上述错误。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题