背景描述
写了一个demo接口,使用postman请求时报错。
@RequestMapping("/hello")
public String helloworld(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = sdf.format(new Date());
return "Hello Qiu Yihang ! 现在的时间是:".concat(format);
}
报错信息:
[2021-07-17 23:29:02] [http-nio-80-exec-5] DEBUG servlet.DispatcherServlet:1127: Exiting from "ERROR" dispatch, status 404
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG servlet.DispatcherServlet:91: GET "/test/hello", parameters={}
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG handler.SimpleUrlHandlerMapping:522: Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG support.OpenEntityManagerInViewInterceptor:86: Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG resource.ResourceHttpRequestHandler:525: Resource not found
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG support.OpenEntityManagerInViewInterceptor:111: Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG servlet.DispatcherServlet:1131: Completed 404 NOT_FOUND
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG servlet.DispatcherServlet:91: "ERROR" dispatch for GET "/error", parameters={}
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG annotation.RequestMappingHandlerMapping:522: Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG support.OpenEntityManagerInViewInterceptor:86: Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG view.ContentNegotiatingViewResolver:349: Selected 'text/html' given [text/html, text/html;q=0.8]
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG support.OpenEntityManagerInViewInterceptor:111: Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
[2021-07-17 23:29:37] [http-nio-80-exec-7] DEBUG servlet.DispatcherServlet:1127: Exiting from "ERROR" dispatch, status 404
解决:
把@ComponentScan注解去掉即可
@SpringBootApplication
@EnableJpaRepositories
@EnableCaching
//@ComponentScan(value = "ltd.hunter.justice.league.shopping.config")
public class JusticeLeagueShoppingApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(JusticeLeagueShoppingApplication.class, args);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PerformanceInterceptor())
.addPathPatterns("/coffee/**").addPathPatterns("/order/**");
}
}