我正在學習課程的活動部分,但遇到了一些障礙。該活動的物件是使用 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) {
}
}
索引.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>
uj5u.com熱心網友回復:
在重新查看我的代碼和作業示例后,我發現我在上面編碼的方式是一種“正確”的做事方式。
該示例有一個可點擊的鏈接,然后顯示路徑,從而生成正確的鏈接。
我正在努力處理的空白頁面只是一個空鏈接。(感覺有點傻)并自動重定向。它解決了這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382185.html
上一篇:空物件反序列化JSONC#
