I have problem with @RestController and Spring Boot application
@RestController
@RequestMapping("/download")
public class CsvExportController {
private final ZipFileService zipFileService;
public CsvExportController(ZipFileService zipFileService) {
this.zipFileService = zipFileService;
}
@GetMapping("/export")
public void getFile(HttpServletResponse response) {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String currentDateTime = dateFormatter.format(new Date());
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=name-" currentDateTime ".zip";
response.setContentType("application/zip");
response.setHeader(headerKey, headerValue);
zipFileService.export(response);
}
@GetMapping("/test")
public String test() {
return "Hello";
}
}
I have problem with GET mapping
2022-01-13 22:15:48.196 WARN 11944 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound : No mapping for GET /download/test
2022-01-13 22:15:48.266 WARN 11944 --- [nio-8080-exec-5] o.s.web.servlet.PageNotFound : No mapping for GET /favicon.ico
First endpoint @GetMapping("/export") works and second doesn;t works. In the latter endpoint, no matter what I return. Here I tested "Hello" but it doesn't work anyway. Interestingly, when I change the path for the first "export" to anything else, it also stops working Any ideas ?
@Configuration
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
//.....
}
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
@SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
CodePudding user response:
Sorry, I can't add a comment. But I think maybe you could provide more details. Because it's available for me. if it's a restapi, the /favicon.ico shouldn't be requested.
CodePudding user response:
The problem is that SpringBoot is probably trying to find a view with the name of Hello and you don't have one. Or have your templates misconfigured.
Try adding @ResponseBody annotation to your method and it should return the string as is:
@GetMapping("/test")
public @ResponseBody String test() {
return "Hello";
}
