import com.sun.net.httpserver.HttpServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;
//指定URI
@Path("/helloworld")
public class HelloWorld {
//處理HTTP的GET請求
@GET
// 處理請求反饋的內容格式爲"text/plain"
@Produces("text/plain")
public String getClichedMessage() {
return "Hello World!";
}
public static void main(String[] args) throws IOException {
//創建RESTful WebService服務
HttpServer server = HttpServerFactory.create("http://192.168.67.28:9999/");
//啓動服務,這會導致新開一個線程
server.start();
//輸出服務的一些提示信息到控制檯
System.out.println("RESTful WebService服務已經啓動");
System.out.println("服務訪問地址: http://192.168.67.28:9999/helloworld");
}
}
<!-- restful jar -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<!-- restful jar -->