当前位置: 代码迷 >> Web前端 >> NetBean跟GlassFish v3 开发Web Service
  详细解决方案

NetBean跟GlassFish v3 开发Web Service

热度:131   发布时间:2012-10-17 10:25:47.0
NetBean和GlassFish v3 开发Web Service
最近在学习Java EE6 ,使用NetBean 6.8和GlassFish v3进行学习开发。对Java EE6 的感觉很不错,它屏蔽了很多细节方面的东东,方便了开发,尤其是Annotation(注解)解脱了配置XML的烦恼。我不想说Annotation多么多么好,所有的技术都有其问题域,都有其触及不到的地方,Java ,.Net 也是如此。

1.开发JAX-WS Web Service 服务端.
   a.使用NetBean 新建一个Web Application
   b. 新建一个Service

  
 
     package  server
     
     import javax.jws.WebService;
     import javax.jws.webMethod;

     @WebService
     public class Hello {
      private String message = new String("Hello, ");
        public void Hello() {
      }

     @WebMethod
     public String sayHello(String name) {
      return message + name + ".";
      }
    }

  


  服务建完后,可以用NetBean提供的测试功能对其进行测试,说实在的这个功能很实用。
  测试服务地址:http://localhost:8080/helloservice/HelloService?Tester
  WSDL地址:http://localhost:8080/helloservice/HelloService?WSDL

2. 开发JAX-WS Web Service 客户端

   a.用NetBean新建Web Application
   b.新建Web Service 客户端
   c.新建Servlet
  

package client;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
import ws.Hello;
import ws.HelloService;

/**
 *
 * @author rywei
 */
@WebServlet(name="WsClient", urlPatterns={"/WsClient"})
public class WsClient extends HttpServlet {
    @WebServiceRef(wsdlLocation="WEB-INF/wsdl/localhost_8080/StudyWeb/HelloService.wsdl")
   private HelloService helloservice;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String s="";
        if (helloservice!=null){
            Hello hello=helloservice.getHelloPort();
            s = hello.sayHello("RenYan Wei");
        }
        PrintWriter out = response.getWriter();

        try {
           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet WsClient</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet WsClient at " + request.getContextPath () + "</h1>");
            out.println("<br>");
            out.println(s);
            out.println("</body>");
            out.println("</html>");
          
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

   

  
  在浏览器内打开http://localhost:8080/WsClient/HelloServlet 进行测试


让人头疼的事是GlassFish v3 内不能在同一个Web Application 同时有客户端与服务端。我建了两个工程,A工程
是服务端,B工程是客户端,这时能正常运行,当我在B工程里建个服务端,要在A工程内进行调用时,就会产生错误,我找了很长时间也不知道是怎么回事,望各位大侠知道的话,欢迎分享,谢谢。

错误如下:

信息: Loading application WSWeb at /WSWeb
信息: WSWeb was successfully deployed in 2,656 milliseconds.
警告: Servlet web service endpoint 'Good' failure
java.lang.IllegalArgumentException: class org.glassfish.webservices.JAXWSServlet has neither @WebService nor @WebServiceProvider annotation
        at com.sun.xml.ws.server.EndpointFactory.verifyImplementorClass(EndpointFactory.java:303)
        at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:120)
        at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:505)
        at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:560)
        at org.glassfish.webservices.JAXWSServlet.registerEndpoint(JAXWSServlet.java:394)
        at org.glassfish.webservices.JAXWSServlet.doInit(JAXWSServlet.java:262)
        at org.glassfish.webservices.JAXWSServlet.init(JAXWSServlet.java:97)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1428)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1060)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:187)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)

警告: StandardWrapperValve[Good]: PWC1382: Allocate exception for servlet Good
javax.servlet.ServletException
        at org.glassfish.webservices.JAXWSServlet.doInit(JAXWSServlet.java:269)
        at org.glassfish.webservices.JAXWSServlet.init(JAXWSServlet.java:97)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1428)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1060)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:187)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException: class org.glassfish.webservices.JAXWSServlet has neither @WebService nor @WebServiceProvider annotation
        at com.sun.xml.ws.server.EndpointFactory.verifyImplementorClass(EndpointFactory.java:303)
        at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:120)
        at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:505)
        at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:560)
        at org.glassfish.webservices.JAXWSServlet.registerEndpoint(JAXWSServlet.java:394)
        at org.glassfish.webservices.JAXWSServlet.doInit(JAXWSServlet.java:262)
        ... 26 more
  相关解决方案