无法通过 @Controller 获取响应,而 @RestController 可以正常工作。

huangapple go评论78阅读模式
英文:

Not able to get response with @Controller, while @RestController works fine

问题

我正试图将项目中的URL /function/hash 映射到特定的HTML页面 html/hashcode.html。这是一个Spring Boot项目,不使用Thymeleaf。

这是我的代码:

// 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 是可以正常工作的,以防你想知道函数的返回值是否不正确。

我还发现在 @RestController 类中使用多层URL,比如 @RequestMapping("/api/test") 是可以正常工作的,但不知何故在上面的这个 @Controller 类中却不起作用。

英文:

I am trying to map the URL /function/hash in my project to a specific HTML page html/hashcode.html. This is a spring boot project without using thymeleaf.

This is my code:

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

The above code returns a 404 when I try to access localhost:8080/function/hash.

I also tried

@Controller
@RequestMapping("/function")
public class FunctionController {
    @RequestMapping("/hash")
    public String hashPage(Model m) {
        return "html/hashcode.html";
    }
}

which also yields a 404 when I go to localhost:8080/function/hash.

Directly using @RequestMapping("/hash") to map the page to /hash works, in case you wonder if the return value of the function is incorrect.

I also find that using multiple layer url like @RequestMapping("/api/test") is working in @RestController classes, but somehow it doesn't work in this @Controller class above.

答案1

得分: 1

返回 `"/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={}


<details>
<summary>英文:</summary>

Return `&quot;/html/hashcode.html&quot;`(prefix `/`), 
and create `&lt;project-root&gt;/src/main/resources/static/html/hashcode.html`

@Controller
public class FunctionController {
@RequestMapping("/function/hash")
public String hashPage(final Model m) {
return "/html/hashcode.html";
}
}


---

When return `&quot;html/hashcode.html&quot;`:

o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/function/html/hashcode.html", parameters={}


On the other hand, when return `&quot;/html/hashcode.html&quot;`:

o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/html/hashcode.html", parameters={}


</details>



# 答案2
**得分**: 0

在请求映射中使用 `path`。

示例:

 `@RequestMapping(path="/hash")`

<details>
<summary>英文:</summary>

use `path` in request mapping. 

Ex:

 `@RequestMapping(path=&quot;/hash&quot;)`

</details>



# 答案3
**得分**: 0

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

```java
@Controller
public class MappingController {

    @RequestMapping("/endpoint1") //返回404
    public String endPoint1() {
        return "Hello endpoint1";
    }

    @RequestMapping("/endpoint2") //因为有@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";
    }
}
英文:

@RestController is combination of @Controller + @ResponseBody. While using @Controller we have to add the @ResponseBody with our methods. You can find more details here

@Controller
public class MappingController {

    @RequestMapping(&quot;/endpoint1&quot;) //returns 404
    public String endPoint1() {
        return &quot;Hello endpoint1&quot;;
    }

    @RequestMapping(&quot;/endpoint2&quot;) //works well because of @ResponseBody
    public @ResponseBody String endPoint2() {
        return &quot;Hello endpoint2&quot;;
    }
}

Add @ResponseBody and these both should work fine

@Controller
public class FunctionController {
    @RequestMapping(&quot;/function/hash&quot;)
    public @ResponseBody String hashPage(Model m) {
        return &quot;html/hashcode.html&quot;;
    }
}

@Controller
@RequestMapping(&quot;/function&quot;)
public class FunctionController {
    @RequestMapping(&quot;/hash&quot;)
    public @ResponseBody String hashPage(Model m) {
        return &quot;html/hashcode.html&quot;;
    }
} 

答案4

得分: 0

添加@ResponseBody注解,@Controller是一个常用的注解,用于将类标记为Spring MVC控制器,而@RestController是一种特殊的控制器,用于RESTFul Web服务,并且相当于@Controller + @ResponseBody。

如果添加@ResponseBody,它将起作用。使用以下代码:

// 包名...;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FunctionController {
    
    @RequestMapping("/function/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

if u add @ResponseBody it will work. use below code

// package ...;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FunctionController {
    
    @RequestMapping(&quot;/function/hash&quot;)
    public @ResponseBody String hashPage(Model m) {
        return &quot;html/hashcode.html&quot;;
    }
}

答案5

得分: 0

如果 HTML 文件是静态资源,请考虑在 Spring Boot 中使用静态内容支持

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

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

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

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

(对于 Spring Webflux 应用程序,请使用 spring.webflux.static-path-pattern

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

英文:

If the html files are static resources, consider the static content support in Spring Boot.

Configure a spring.resources.static-locations to specify the resource localtions.

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

And set the mapping pattern if you do not want to map it the root path.

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

(Or spring.webflux.static-path-pattern for Spring webflux application)

Now you can view the resources via http://localhost:8080/content/some.html

huangapple
  • 本文由 发表于 2020年5月30日 11:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/62097557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定