Веб-сервисы консультируют Java - PullRequest
1 голос
/ 24 ноября 2011

Я читаю книгу о Java и веб-сервисах, но застрял в первом примере книги. Посмотрите на эти коды, и, пожалуйста, скажите мне, как бы вы пошли на выполнение этих классов и как или где бы вы их сохранили. Без использования любой 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 / "и здесь три класса"

Ответы [ 2 ]

2 голосов
/ 24 ноября 2011

Насколько мне известно, файл интерфейса и файл impl могут находиться в разных папках (в папке src), но путь к файлу интерфейса @WebService (endpointInterface = "ch01.ts.TimeServer") должен быть указан правильно. Как только этот путь верен, публикация должна произойти, и wsdl должен быть сгенерирован.

1 голос
/ 24 ноября 2011

Если вы хотите изучать веб-сервисы, я предлагаю вам загрузить существующий фреймворк, такой как Apache CXF или Apache axis2 . Они включают в себя множество примеров, которые вы можете скомпилировать и запустить. Образцы легко понять, и у вас будет что-то, что работает. Они также дают вам достаточно хорошую структуру для проекта, так что вы знаете, куда поместить файлы xml и wsdl и т. Д.

И да, вам не нужна IDE для их запуска. Исходя из моего опыта, лучше начать без IDE, чтобы вы точно знали, что происходит. IDE повысит вашу производительность позже.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...