简体   繁体   中英

Web Services Advise Java

I am reading a book about Java and Web Services, but I am stuck in the first example of the book. Look at this codes, and please tell me how would you go for running this classes and how or where would you save them. With out using any IDE!

Time Server class

    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 package 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(); }
     }

and then the last class the TimeServerPublisher package 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());
 }
 }

I know how to compile them. But something goes wrong when i try to run the publisher.

The way that i have saved them is in a folder called Web/ts/"and here the three classes"

As far as I know, interface file and the impl file can be in different folders(under your src folder), but the @WebService(endpointInterface = "ch01.ts.TimeServer") path to the interface file should be shown correctly. Once this path is correct, the publishing should happend and the wsdl should get generated.

If you want to learn Webservices I suggest you download an existing framework like Apache CXF or Apache axis2 . They include many samples which you can compile and run. The samples are easy to understand and you will have from the beginning something that works. They also give you reasonably good structure for the project, so you know where to put xml and wsdl files, etc.

And yes, you don't need an IDE to run them. From my experience it's better to start without IDE, so you know exactly what is going on. An IDE will boost your productivity later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM