简体   繁体   中英

Spring Boot: Controller - 404 Not Found

I have this webapp

Here is the 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");

    }

}

SpringBootApplication:

@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/");
    }

}

The structure:

com
 - web
 - - foo
 - - - controller
 - - - - Update.java
 - - - MainCore.java
 - - - AppInitializer.java
 - - - WebConfig.java

When I access localhost:8080/update/tracking_number it works perfect.

But when I access localhost:8080/update/order_products it no longer works and gives the response:

{
    "timestamp": 1618404297125,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/update/order_products"
}

Can you check if the request has Content-Type header. Also in @RequestMapping add consumes = "application/text" or "application/json" whatever is relevant.

Try add @ResponseBody methods of updateOrderProducts

The project is running directly from Intellij IDEA.

So, in my case, the solution was to Invalidate caches .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM