import com.sun.jersey.api.container.httpserver.HttpServerFactory;
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://localhost:9999/");
//启动服务,这会导致新开一个线程
server.start();
//输出服务的一些提示信息到控制台
System.out.println("RESTful WebService服务已经启动");
System.out.println("服务访问地址: http://localhost:9999/helloworld");
}
}
HttpServerFactory.create("http://localhost:9999/");
localhost : 是本地域名 , 或者其他已经绑定的合法域名:
9999 端口号, 自己随便设置: 只要不冲突就行了
==================================
在浏览器中http://localhost:9999/helloworld 可以看到效果