netbeans 成功测试后,Chrome中未显示REST风格的响应

3okqufwl  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(151)

我正在学习课程的活动部分,遇到了一个小障碍。活动的目标是使用NetBeans IDE的一个restful服务来显示一个文本字符串。
当我在Netbeans中运行TEST RESTful Web服务选项时,它起作用了:

但是,当我运行程序时,在浏览器中看到的只是一个空白页:

一开始我以为我的编码不正确,所以我重新做了一遍,但结果还是一样。最后一次尝试后,我打开了解决方案文件,得到了正确的代码,但解决方案代码显示了一个输出,而我的代码仍然没有。为什么浏览器没有显示字符串的路径?
如果我直接在Chrome中输入路径,它会完全按照它应该做的那样显示。
然后,我尝试添加一个指向index.html文件的重定向,这实现了练习的预期结果,但我不认为这符合问题的精神:

我相信有一个“适当”的方法来做这件事,但是我不能解决它。下面是我的代码:
RestService.java

package restService;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;

/**
 * REST Web Service
 *
 * @author Matthew
 */
@Path("rest")
public class RestSevice {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of RestSevice
     */
    public RestSevice() {
    }

    /**
     * Retrieves representation of an instance of restService.RestSevice
     * @return an instance of java.lang.String
     */
    @GET
    @Path("/banner")
    @Produces(MediaType.TEXT_HTML)
    public String getHtml() {
        return "<HTML><body><h1>This is a RESTful response!</h1></<body></html>";
    }

    /**
     * PUT method for updating or creating an instance of RestSevice
     * @param content representation for the resource
     */
    @PUT
    @Consumes(javax.ws.rs.core.MediaType.TEXT_PLAIN)
    public void putText(String content) {
    }
}

index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>RESTful service</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <ul>
                <meta http-equiv="Refresh" content="0; url='http://localhost:8080/RESTservice/webresources/rest/banner'" />
            </ul>
        </div>
    </body>
</html>
dvtswwa3

dvtswwa31#

在重新查看了我的代码和工作过的例子之后,我发现我上面的编码方式是一种“正确”的做事方式。
这个例子有一个可点击的链接,然后显示路径,得到正确的链接。
我正在挣扎的空白页面,只是一个空的链接。(有点觉得有点愚蠢)和自动重定向。它解决了这个问题。

相关问题