加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Web服务如何工作

发布时间:2020-12-14 05:51:46 所属栏目:Java 来源:网络整理
导读:我是webservices的新手,我想在我的 eclipse项目中使用 java来实现webservices. 所以任何人都可以告诉我如何实施和创建项目 谢谢 解决方法 如W3C所定义的,Web服务是一种支持可互操作的机器到机器交互的软件系统 通过网络.更精细的是,系统消耗来自其他软件系统
我是webservices的新手,我想在我的 eclipse项目中使用 java来实现webservices.

所以任何人都可以告诉我如何实施和创建项目

谢谢

解决方法

如W3C所定义的,Web服务是一种支持可互操作的机器到机器交互的软件系统
通过网络.更精细的是,系统消耗来自其他软件系统的服务.

Web服务有两大类:

> REST兼容
>任意网络服务

要实现Web服务,需要根据他/她的要求选择一个类别.
Java有一堆APIS来实现这两个类别的Web服务.

实施Web服务之前的要求是:

> XML
> WSDL(Web服务描述语言)
> SOAP协议等

与其他类别相比,基于REST的实现有点容易实现.因此,最好从REST投诉Web服务开始.

Web服务的工作原理:

WS作为请求 – 响应范例起作用,有一个实体将向其特定对应方(服务提供方实体)请求某些服务.根据请求,服务提供商将回复响应消息.因此,有两个消息涉及一个请求消息(XML)和一个响应消息(XML).有很多方法可以实现这些目标.细节可以在web service architecture找到

初学者可以从JERSEY jsr311标准参考实现开始构建RESTful Web服务.

示例(特定于运动衫):

第一步:创建根资源

// 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.wadlnTry out %shelloworldnHit enter to stop it...",BASE_URI,BASE_URI));
      System.in.read();
      httpServer.stop();
  }

}

REST REFERENCE – by Roy T . Fielding

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读