Reputation: 21
I have a Spring Web MVC application with MongoDB, which stores files in Azure Blob Storage. When I send a POST request with a zip file to be stored in Postman I receive Status Code 404 Not found. My logs look like this:
2022-05-21 06:18:05.421 DEBUG 11816 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
2022-05-21 06:18:18.595 DEBUG 11816 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : POST "/api/upload", parameters={multipart}
2022-05-21 06:18:18.604 DEBUG 11816 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-05-21 06:18:18.608 DEBUG 11816 --- [nio-8080-exec-3] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-05-21 06:18:18.608 DEBUG 11816 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-05-21 06:18:18.609 DEBUG 11816 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={multipart}
2022-05-21 06:18:18.609 DEBUG 11816 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2022-05-21 06:18:18.610 DEBUG 11816 --- [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json, application/xml;charset=UTF-8, text/xml;charset=UTF-8, application/*+xml;charset=UTF-8, application/xml;charset=UTF-8, text/xml;charset=UTF-8, application/*+xml;charset=UTF-8]
2022-05-21 06:18:18.610 DEBUG 11816 --- [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Sat May 21 06:18:18 CEST 2022, status=404, error=Not Found, message=No message available, (truncated)...]
2022-05-21 06:18:18.611 DEBUG 11816 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
I have the following classes:
CalibrationController.java
@Controller
public class CalibrationController {
private final CalibrationService calService;
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(CalibrationController.class);
@Autowired
public CalibrationController (CalibrationService calService) {
this.calService = calService;
}
@PostMapping(path = "/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@ResponseBody
public ResponseEntity<String> newBundle(@RequestParam("file") MultipartFile file) {
log.info("Received a file to upload");
Calibration created = calService.uploadBundle(file);
return ResponseEntity.status(HttpStatus.CREATED).body("Bundle was saved successfully with URI: " + created.getUrl());
}
}
CalibrationInitializer.java
public class CalibrationInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext ServletContext){
// Load Spring web application configuration
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(CalibrationServletConfig.class);
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic registration = ServletContext.addServlet("api", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/api/*");
}
}
CalibrationServletConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example.prototype_mvc.databaseStorage.controller"})
public class CalibrationServletConfig {
}
I've tried to create the implementation for Dispatcher Servlet as a possible solution for this error because I was getting it also without the CalibrationInitialize.java and CalibrationConfig.java classes. So shouldn't my application run also without initialization and registration of Dispatcher Servlet, if I am having annotated controller? Shouldn't the annotations in the Controller.java class be recognized?
I really don't know what I am doing wrong. Any help would be useful!
Upvotes: 1
Views: 697
Reputation: 367
server.servlet.context-path=/api
public class CalibrationInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext ServletContext){
// Load Spring web application configuration
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(CalibrationServletConfig.class);
}
}
Upvotes: 0