簡體   English   中英

Web服務建議Java

[英]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 CXFApache axis2 它們包括許多可以編譯和運行的示例。 這些示例很容易理解,從一開始您就可以使用一些有用的東西。 它們還為項目提供了相當好的結構,因此您知道將xml和wsdl文件放在何處等。

是的,您不需要IDE即可運行它們。 根據我的經驗,最好不要使用IDE,因此您確切知道發生了什么。 IDE將在以后提高您的生產力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM