[英]Web Services Advise Java
我正在阅读一本有关Java和Web服务的书,但我陷入了书的第一个示例中。 看看这些代码,请告诉我你将如何运行这些课程以及如何或在何处保存它们。 没有使用任何IDE!
时间服务器类
package web.ts; // time server
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
/**
* The annotation @WebService signals that this is the
* SEI (Service Endpoint Interface). @WebMethod signals
* that each method is a service operation.
*
* The @SOAPBinding annotation impacts the under-the-hood
* construction of the service contract, the WSDL
* (Web Services Definition Language) document. Style.RPC
* simplifies the contract and makes deployment easier.
*/
@WebService
@SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer {
@WebMethod String getTimeAsString();
@WebMethod long getTimeAsElapsed();
}
TimeServerImpl包web.ts;
import java.util.Date;
import javax.jws.WebService;
/**
* The @WebService property endpointInterface links the
* SIB (this class) to the SEI (ch01.ts.TimeServer).
* Note that the method implementations are not annotated
* as @WebMethods.
*/
@WebService(endpointInterface = "ch01.ts.TimeServer")
public class TimeServerImpl implements TimeServer {
public String getTimeAsString() { return new Date().toString(); }
public long getTimeAsElapsed() { return new Date().getTime(); }
}
然后是最后一个类TimeServerPublisher包web.ts;
import javax.xml.ws.Endpoint;
/**
* This application publishes the web service whose
* SIB is ch01.ts.TimeServerImpl. For now, the
* service is published at network address 127.0.0.1.,
* which is localhost, and at port number 9876, as this
* port is likely available on any desktop machine. The
* publication path is /ts, an arbitrary name.
*
* The Endpoint class has an overloaded publish method.
* In this two-argument version, the first argument is the
* publication URL as a string and the second argument is
* an instance of the service SIB, in this case
* ch01.ts.TimeServerImpl.
*
* The application runs indefinitely, awaiting service requests.
* It needs to be terminated at the command prompt with control-C
* or the equivalent.
*
* Once the applicatation is started, open a browser to the URL
*
* http://127.0.0.1:9876/ts?wsdl
*
* to view the service contract, the WSDL document. This is an
* easy test to determine whether the service has deployed
* successfully. If the test succeeds, a client then can be
* executed against the service.
*/
public class TimeServerPublisher {
public static void main(String[ ] args) {
// 1st argument is the publication URL
// 2nd argument is an SIB instance
Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
}
}
我知道如何编译它们。 但是当我尝试运行发布者时出了点问题。
我保存它们的方式是在一个名为Web / ts /的文件夹中,这里是三个类。
据我所知,接口文件和impl文件可以在不同的文件夹中(在你的src文件夹下),但是应该正确显示接口文件的@WebService(endpointInterface =“ch01.ts.TimeServer”)路径。 一旦此路径正确,就应该进行发布并生成wsdl。
如果你想学习Webservices,我建议你下载一个现有的框架,如Apache CXF或Apache axis2 。 它们包括许多可以编译和运行的示例。 这些示例很容易理解,从一开始您就可以使用一些有用的东西。 它们还为项目提供了相当好的结构,因此您知道将xml和wsdl文件放在何处等。
是的,您不需要IDE即可运行它们。 根据我的经验,最好不要使用IDE,因此您确切知道发生了什么。 IDE将在以后提高您的生产力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.