无法在名称为 'spring' 的 servlet 中解析名称为 'streamrecords' 的视图

Could not resolve view with name 'streamrecords' in servlet with name 'spring'

我正在使用来自控制器示例的简单流整数,使用 Spring 4、Java 8、Tomcat 7.

代码:

package com.entrib.emg.server.controller.rest;
import java.io.IOException;
import java.io.OutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

@Controller
public class StreamRecordsController {

    @RequestMapping(value = "/streamrecords", method = RequestMethod.GET)
    public StreamingResponseBody handleRequest ()
            throws Exception {

        return new StreamingResponseBody() {
            @Override
            public void writeTo (OutputStream out) throws IOException {
                for (int i = 0; i < 1000; i++) {
                    out.write((Integer.toString(i) + " - ")
                                        .getBytes());
                    out.flush();
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }
}

错误:

Type Exception Report

Message Could not resolve view with name 'streamrecords' in servlet with name 'spring'

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Could not resolve view with name 'streamrecords' in servlet with name 'spring'
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1266)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.

下面是快照:

问题:

我在这里错过了什么。

期望:

这只是一个简单的剩余 API 代码,当从浏览器或邮递员点击 API 时,我希望从该控制器流出一个整数流。

---------------------------------------- --------

已更新

---------------------------------------- --------

当我添加 @ResponseBody 时,出现以下错误,

Type Status Report

Description The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.

您的 servlet 方法中缺少 @ResponseBody 或者您可以使用 @RestController

标记您的 class

只需将您的 class 的注释更改为 @RestController