请确认:
-
find 方法返回的值是一个值为 "list" 的字符串,并且 find 方法声明正在等待 RestResponse 对象
例如,如果我有一个像这样的RestResponse 对象:
public class RestResponse {
private String value;
public RestResponse(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}
然后尝试以这种方式返回值:
public RestResponse find(@PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return new RestResponse("list");
}
-
验证该方法是否具有 @RequestMapping 注释以及您期望从 url 获得的值
@RequestMapping(method = RequestMethod.GET, value = "/{name}")
默认情况下,调用 rest 资源的正确方法是通过您在 @RestController 级别 (@RequestMapping("/test")) 设置的 @RequestMapping 值,在这种情况下可能是:http://localhost:8080/test/myValue
如果您需要使用不同的上下文路径,那么您可以在 application.properties 上更改它(用于 spring boot)
server.contextPath=/MyProject/wangdu
在这种情况下,您可以像这样调用 api:
http://localhost:8080/MyProject/wangdu/test/myValue
这是此替代方案的完整代码:
@RestController
@RequestMapping("/test")
public class AreaController {
@RequestMapping(method = RequestMethod.GET, value = "/{name}")
public RestResponse find(@PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return new RestResponse("list");
}