一、前言
最近写java项目,springboot框架,碰到了这个小问题。
写了一个MyController.java
,代码如下:
@Controller
@RequestMapping(path = "/my")
public class MyController {
Logger logger = LoggerFactory.getLogger(MyController.class);
/**
* 测试接口
*/
@SuppressWarnings("unchecked")
@RequestMapping("/find")
public JSONObject findClassExamCount(HttpServletRequest request) throws ParseException {
logger.info("--------------/my/find---------------");
JSONObject jsonObject = new JSONObject();
jsonObject.put("code","1");
return jsonObject;
}
然后用postman调用http://localhost:8080/my/find
时,就报错了:
{
"timestamp": "2023-08-02T10:03:02.774+0800",
"status": 500,
"error": "Internal Server Error",
"message": "Error resolving template [my/find], template might not exist or might not be accessible by any of the configured Template Resolvers",
"trace": "org.thymeleaf.exceptions.TemplateInputException: Error resolving template [my/find], template might not exist or might not be accessible by any of the configured Template Resolvers\r\n\tat org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)\r\n\tat org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)\r\n\tat org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)\r\n\tat org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)\r\n\tat org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)\r\n\tat org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)\r\n\tat org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)\r\n\tat org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)\r\n\tat org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.lang.Thread.run(Thread.java:748)\r\n",
"path": "/my/find"
}
打断点看了下,到return
方法为止还是没有报错的,但是后续到框架方法里就报错了;
主要报错信息就是:
template might not exist or might not be accessible by any of the configured Template Resolvers
二、解决方法
1.其实,就是把@Controller
改成@RestController
就可以了。如下:
@RestController
@RequestMapping(path = "/my")
public class MyController {
Logger logger = LoggerFactory.getLogger(MyController.class);
/**
* 测试接口
*/
@SuppressWarnings("unchecked")
@RequestMapping("/find")
public JSONObject findClassExamCount(HttpServletRequest request) throws ParseException {
logger.info("--------------/my/find---------------");
JSONObject jsonObject = new JSONObject();
jsonObject.put("code","1");
return jsonObject;
}
2.或者,在接口上加@ResponseBody
也可以,如下:
@Controller
@RequestMapping(path = "/my")
public class MyController {
Logger logger = LoggerFactory.getLogger(MyController.class);
/**
* 测试接口
*/
@SuppressWarnings("unchecked")
@RequestMapping("/find")
@ResponseBody
public JSONObject findClassExamCount(HttpServletRequest request) throws ParseException {
logger.info("--------------/my/find---------------");
JSONObject jsonObject = new JSONObject();
jsonObject.put("code","1");
return jsonObject;
}
3.使用postman调用,返回值就正常了:
{
"code": "1"
}