![](/img/trans.png)
[英]Why Spring Boot controller test returning 404, but controller found in context?
[英]Spring Boot: Controller - 404 Not Found
我有这个网络应用程序
这是 controller:
@Controller
@RequestMapping(value = "/update")
public class Update{
@RequestMapping(value = "/tracking_number", method = RequestMethod.POST)
public ResponseEntity<String> updateTrackingNumber(@RequestHeader(value = "order_id")String orderId,
@RequestHeader(value = "tracking_number")String trackingNumber,
HttpSession httpSession){
//url: localhost:8080/update/tracking_number
//this one works perfectly
}
@RequestMapping(value = "/order_products", method = RequestMethod.POST)
public ResponseEntity<String> updateOrderProducts(){
return ResponseEntity.ok().body("i hope to see this text");
}
}
SpringBoot应用程序:
@SpringBootApplication
public class MainCore extends SpringBootServletInitializer{
public static void main(String[] args){
SpringApplication.run(MainCore.class, args);
}
}
WebApplicationInitializer:
public class AppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext container) throws ServletException{
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.scan("com.web.foo");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("mvc", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
WebMvcConfigurer:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.web.foo.controller")
public class WebConfig implements WebMvcConfigurer{
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/jsp/");
bean.setSuffix(".jsp");
return bean;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
结构:
com
- web
- - foo
- - - controller
- - - - Update.java
- - - MainCore.java
- - - AppInitializer.java
- - - WebConfig.java
当我访问localhost:8080/update/tracking_number
时,它运行良好。
但是当我访问localhost:8080/update/order_products
它不再起作用并给出响应:
{
"timestamp": 1618404297125,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/update/order_products"
}
您能否检查请求是否具有 Content-Type header。 同样在 @RequestMapping 添加消费 = "application/text" 或 "application/json" 任何相关的内容。
尝试添加 updateOrderProducts 的 @ResponseBody 方法
该项目直接从 Intellij IDEA 运行。
因此,就我而言,解决方案是Invalidate caches
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.