我是 web 服务的新手,我想在我的 eclipse 项目中使用 java 实现 web 服务。
那么谁能告诉我如何实现和创建项目
谢谢
最佳答案
根据 W3C 的定义,网络服务是一种支持可互操作的机器对机器交互的软件系统 通过网络。更详细地说,系统使用来自其他软件系统的服务。
Web 服务有两大类:
- 符合 REST 标准
- 任意网络服务
要实现 Web 服务,需要根据他/她的要求选择一个类别。 Java 有一堆 APIS 可以在这两个类别上实现 Web 服务。
实现网络服务之前的要求是:
- XML
- WSDL(网络服务描述语言)
- SOAP协议(protocol)等
与其他类别相比,基于 REST 的实现起来有点容易。所以最好从 REST 兼容的 Web 服务开始。
网络服务的工作原理:
WS 作为请求-响应范式工作,有一个实体会向它的特定对应方(即服务提供者实体)请求某些服务。根据请求,服务提供商将响应一条响应消息。因此涉及两条消息,一条是请求消息(XML),一条是响应消息(XML)。有很多方法可以实现这些。详情可以在web service architecture找到
初学者可以从JERSEY开始构建 RESTful Web 服务的 jsr311 标准引用实现。
示例( Jersey 特定):
第一步:创建根资源
// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {
@GET
@Produces("text/plain")
public String getClichedMessage() {
return "Hello World";
}
}
第二步:部署
public class Main {
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(8080).build();
}
public static final URI BASE_URI = getBaseURI();
protected static HttpServer startServer() throws IOException {
System.out.println("Starting ...");
ResourceConfig resourceConfig = new PackagesResourceConfig("com.sun.jersey.samples.helloworld.resources");
return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
}
public static void main(String[] args) throws IOException {
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
BASE_URI, BASE_URI));
System.in.read();
httpServer.stop();
}
关于java - 网络服务如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4544181/