无法获得@Controller的响应,而@RestController正常工作

问题描述 投票:1回答:5

我正在尝试将项目中的URL /function/hash映射到特定的HTML页面html/hashcode.html。这是一个不使用百里香叶的春季靴子项目。

这是我的代码:

// 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";
    }
}

当我转到localhost:8080/function/hash时也会产生404。

如果您想知道函数的返回值是否不正确,请直接使用@RequestMapping("/hash")将页面映射到/hash

[我还发现,使用@RequestMapping("/api/test")之类的多层url在@RestController类中是可行的,但是以某种方式在上面的@Controller类中不起作用。

java spring spring-boot rest spring-mvc
5个回答
0
投票

在请求映射中使用path

Ex:

@RequestMapping(path="/hash")


0
投票

返回"/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";
    }
}

0
投票

添加@ResponseBody注释,@Controller是一个常用注释,用于将类标记为Spring MVC Controller,而@RestController是RESTFul Web服务中使用的特殊控制器,等效于@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";
    }
}

0
投票

@RestController@Controller + @ResponseBody的组合。在使用@Controller时,我们必须在方法中添加@ResponseBody。您可以找到更多详细信息here

@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";
    }
} 

0
投票

如果html文件是静态资源,请在Spring Boot中考虑the static content support

配置spring.resources.static-locations以指定资源位置。

spring.resources.static-locations=file:/opt/files/,classpath:/static-files

如果不想将其映射到根路径,请设置映射模式。

pring.mvc.static-path-pattern=/content/**

(或对于Spring webflux应用程序为spring.webflux.static-path-pattern

现在您可以通过http://localhost:8080/content/some.html查看资源>

© www.soinside.com 2019 - 2024. All rights reserved.