我對@RestController 和 Spring Boot 應用程式有疑問
@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";
}
}
我對 GET 映射有疑問
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
第一個端點 @GetMapping("/export") 有效,第二個無效;無效。在后一個端點,無論我回傳什么。在這里我測驗了“你好”,但它無論如何都不起作用。有趣的是,當我將第一個“匯出”的路徑更改為其他任何東西時,它也停止作業有什么想法嗎?
@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);
}
}
uj5u.com熱心網友回復:
抱歉,我無法添加評論。但我想也許你可以提供更多細節。因為它對我可用。如果它是 a restapi,則/favicon.ico不應請求 。
uj5u.com熱心網友回復:
您可以嘗試將測驗端點的回應型別設定為 json
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
uj5u.com熱心網友回復:
問題是 SpringBoot 可能正在嘗試查找名稱為的視圖,Hello而您沒有。或者您的模板配置錯誤。
嘗試向@ResponseBody您的方法添加注釋,它應該按原樣回傳字串:
@GetMapping("/test")
public @ResponseBody String test() {
return "Hello";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/412295.html
標籤:
上一篇:如何在SpringBoot中隱藏/profile端點
下一篇:asp.net核心路由變數
