Programming in C#(.NET) and Java Web Services


不论是在 .NET 或 Java, 一个 WebService Class与一般的 Class 基本上没有太大差
异. 在 .NET 中, 最明显的就是 Metadata/Attribute 的使用. 就以一最简单的
Hollo Web Service 为例:
1
2
3
4
5
6
7
8
C#
using System.Web.Services;
public class Hello{
[ WebMethod ]
public string SayHey() {
return “Hello”;
}
}

Method SayHey() 上头的 [ WebMethod ] 就是这个 method 的 Attribute (Metadata
Annotation). 意味著这个 method 将可以透过 "Web"来沟通.
也就是说这个 method 会对应到一个 WSDL Operation.

注: WSDL 可以说是一个 WebSerivce 的介面(Interface), 以XML的格式呈现,
一般来说, 由WebService Tool/Platform自动产生.

Annotation的使用可以说是"目前".NET 与 Java 在Programmming上的主要差异.
在 Java(JAX-RPC) 中我们用一个 interface 来定义哪几个 method 要成为
WSDL Operation.
JAX-RPC 要求这个interface 必须要 extend java.rmi.Remote.
(并且每个method 要throws java.rmi.RemoteException),
在JSR109(J2EE WebServices)中这样的 interface 叫
Service Endpoint Interface (SEI):
1
2
3
4
5
package hello;
public interface Hello extends java.rmi.Remote {
public String sayHey() throws
java.rmi.RemoteException;
}

然后我们根据这个 SEI 发展出 implementaion.
只有在 SEI 上宣告的 method 才会成为 WSDL上的 Operation.
在 J2EE 平台上, JSR109 允许 implementaion class 可以不用 implement SEI,
但必须提供所有 method signature 相同的 methods.
1
2
3
4
5
6
7
package hello;
public class HelloImpl implements Hello {
public String sayHey() throws
java.rmi.RemoteException {
return "Hey!";
}
}

在不久的未来, Java 也将会支援 Metadata:
JSR-175(Metadata Facility for the JavaTM Programming Language)
将为Java 加入 Metadata Attributes 的支援.

JSR-181 Web Services Metadata (lead by BEA)定义了 WebServices Specific Attributes
然而这些很可能也成为JSR-224 JAX-RPC 2.0(lead by SUN) 的一部分
1
2
3
4
5
6
7
package hello;
@WebService public class Hello {
@Remote public String sayHey() throws
java.rmi.RemoteException {
return "Hey!";
}
}

不论是在 .NET 或 Java, 一个 WebService Class与一般的 Class 基本上没有太大差
异. 在 .NET 中, 最明显的就是 Metadata/Attribute 的使用. 就以一最简单的
Hollo Web Service 为例:
1
2
3
4
5
6
7
8
C#
using System.Web.Services;
public class Hello{
[ WebMethod ]
public string SayHey() {
return “Hello”;
}
}

Method SayHey() 上头的 [ WebMethod ] 就是这个 method 的 Attribute (Metadata
Annotation). 意味著这个 method 将可以透过 "Web"来沟通.
也就是说这个 method 会对应到一个 WSDL Operation.

注: WSDL 可以说是一个 WebSerivce 的介面(Interface), 以XML的格式呈现,
一般来说, 由WebService Tool/Platform自动产生.

Annotation的使用可以说是"目前".NET 与 Java 在Programmming上的主要差异.
在 Java(JAX-RPC) 中我们用一个 interface 来定义哪几个 method 要成为
WSDL Operation.
JAX-RPC 要求这个interface 必须要 extend java.rmi.Remote.
(并且每个method 要throws java.rmi.RemoteException),
在JSR109(J2EE WebServices)中这样的 interface 叫
Service Endpoint Interface (SEI):
1
2
3
4
5
package hello;
public interface Hello extends java.rmi.Remote {
public String sayHey() throws
java.rmi.RemoteException;
}

然后我们根据这个 SEI 发展出 implementaion.
只有在 SEI 上宣告的 method 才会成为 WSDL上的 Operation.
在 J2EE 平台上, JSR109 允许 implementaion class 可以不用 implement SEI,
但必须提供所有 method signature 相同的 methods.
1
2
3
4
5
6
7
package hello;
public class HelloImpl implements Hello {
public String sayHey() throws
java.rmi.RemoteException {
return "Hey!";
}
}

在不久的未来, Java 也将会支援 Metadata:
JSR-175(Metadata Facility for the JavaTM Programming Language)
将为Java 加入 Metadata Attributes 的支援.

JSR-181 Web Services Metadata (lead by BEA)定义了 WebServices Specific Attributes
然而这些很可能也成为JSR-224 JAX-RPC 2.0(lead by SUN) 的一部分
1
2
3
4
5
6
7
package hello;
@WebService public class Hello {
@Remote public String sayHey() throws
java.rmi.RemoteException {
return "Hey!";
}
}

posted @ 2017-03-23 14:12  欣欣王子  阅读(140)  评论(0)    收藏  举报