![](/img/trans.png)
[英]Spring security permitall() works for @RestController but not for @Controller
[英]Not able to get response with @Controller, while @RestController works fine
I am trying to map the URL /function/hash
in my project to a specific HTML page html/hashcode.html
. 這是一個不使用 thymeleaf 的 spring 引導項目。
這是我的代碼:
// package ...;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FunctionController {
@RequestMapping("/function/hash")
public String hashPage(Model m) {
return "html/hashcode.html";
}
}
當我嘗試訪問localhost:8080/function/hash
時,上面的代碼返回 404。
我也試過
@Controller
@RequestMapping("/function")
public class FunctionController {
@RequestMapping("/hash")
public String hashPage(Model m) {
return "html/hashcode.html";
}
}
當我 go 到localhost:8080/function/hash
時,這也會產生 404。
直接使用@RequestMapping("/hash")
到 map 到/hash
的頁面可以工作,以防您懷疑 function 的返回值是否不正確。
我還發現使用多層 url 像@RequestMapping("/api/test")
在@RestController
類中工作,但不知何故它在上面的這個@Controller
class 中不起作用。
返回"/html/hashcode.html"
(前綴/
),並創建<project-root>/src/main/resources/static/html/hashcode.html
@Controller
public class FunctionController {
@RequestMapping("/function/hash")
public String hashPage(final Model m) {
return "/html/hashcode.html";
}
}
當返回"html/hashcode.html"
時:
o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/function/html/hashcode.html", parameters={}
另一方面,當返回"/html/hashcode.html"
時:
o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/html/hashcode.html", parameters={}
在請求映射中使用path
。
前任:
@RequestMapping(path="/hash")
@RestController
是@Controller + @ResponseBody
的組合。 在使用@Controller
時,我們必須在我們的方法中添加@ResponseBody
。 您可以在此處找到更多詳細信息
@Controller
public class MappingController {
@RequestMapping("/endpoint1") //returns 404
public String endPoint1() {
return "Hello endpoint1";
}
@RequestMapping("/endpoint2") //works well because of @ResponseBody
public @ResponseBody String endPoint2() {
return "Hello endpoint2";
}
}
添加@ResponseBody
,這些都應該可以正常工作
@Controller
public class FunctionController {
@RequestMapping("/function/hash")
public @ResponseBody String hashPage(Model m) {
return "html/hashcode.html";
}
}
@Controller
@RequestMapping("/function")
public class FunctionController {
@RequestMapping("/hash")
public @ResponseBody String hashPage(Model m) {
return "html/hashcode.html";
}
}
add @ResponseBody annotation, The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while the @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody
如果您添加 @ResponseBody 它將起作用。 使用下面的代碼
// package ...;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FunctionController {
@RequestMapping("/function/hash")
public @ResponseBody String hashPage(Model m) {
return "html/hashcode.html";
}
}
如果 html 文件是 static 資源,請考慮 Z38008DD81C2F4D7985ECF61 中的 static 內容支持。
配置spring.resources.static-locations
以指定資源位置。
spring.resources.static-locations=file:/opt/files/,classpath:/static-files
如果您不想將 map 設置為根路徑,請設置映射模式。
pring.mvc.static-path-pattern=/content/**
(或 Spring webflux 應用程序spring.webflux.static-path-pattern
)
現在您可以通過http://localhost:8080/content/some.html查看資源
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.