`

jee6 学习笔记 7.1 - @WebService

    博客分类:
  • JEE
阅读更多
Exposing an EJB as web service is just a matter of a few annotations.

This example exposes an ejb as standard SOAP web services. It uses default JAX-WS 2.1.6, which is part of the JDK1.6 distribution now.

It has two approaches:

1. Define a service interface and apply the @WebService on the interface. All the public methods would be exposed as web services.

2. Apply the @WebService to the implementation class directly, without an service interface. If you don't want to expose a public method in the class, apply @WebMethod(exclude=true) to the method.

here's the ejb3.1 implementation class StudentDAOImp after the "find" method exposed as a web service:
package com.jxee.ejb.student;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.jxee.ejb.usr.UserDAOImp;
import com.jxee.model.student.Student;

/**
 * Rules of usage of @javax.jws.WebService
 * 
 * If the @WebService annotation of an service implementation class references 
 * a SEI(Service Endpoint Interface), the implementation class must not have 
 * any @WebMethod annotations.
 * 
 * All public methods for an SEI are considered exposed methods regardless of 
 * whether the @WebMethod annotation is specified or not.
 *  
 * It is incorrect to have an @WebMethod annotation on an SEI that contains the 
 * exclude attribute.
 * 
 * For an implementation class that does not reference an SEI, if the @WebMethod
 * annotation is specified with a value of exclude=true, that method is not exposed.
 * 
 * If the @WebMethod annotation is not specified, all public methods are exposed
 * including the inherited methods, except for that inherited from java.lang.Object.
 */
@Stateless
@WebService
public class StudentDAOImp implements StudentDAO {
  
  private static final Logger log = Logger.getLogger(UserDAOImp.class);

  @PersistenceContext(unitName="punit.projee6")
  private EntityManager em;
  
  @WebMethod
  public List<Student> find(@WebParam(name="name") String nameFilter, 
                            @WebParam(name="max") int max) {
    String pattern = nameFilter != null ? "%" + nameFilter + "%" : "%";
    String sql  = "select s from Student s where s.name like :pattern";
    Query q = em.createQuery(sql);
    q.setParameter("pattern", pattern);
    q.setFirstResult(0);
    q.setMaxResults(max);
    return q.getResultList();
  }
  
  @WebMethod(exclude=true)
  public void add(Student stu) {
    log.debug("adding new student: " + stu);
    if(stu.getId() != null) {
      // set null to avoid exception "org.hibernate.PersistentObjectException: detached entity passed to persist"
      stu.setId(null);
    }
    em.persist(stu);
  }
  
  @WebMethod(exclude=true)
  public void update(Student stu) {
    log.debug("updating student: " + stu);
    em.merge(stu);
  }
  
  @WebMethod(exclude=true)
  public void delete(Student stu) {
    Student todel = em.find(Student.class, stu.getId());
    log.debug("student loaded to delete: " + todel);
    em.remove(todel);
  }
}


Since web services are normally deployed as web applications, The wsdl url of the deployed web service has this pattern: http://server:port/context/service?wsdl

JBoss and Glassfish provide admin screens to display the wsdl url. In JBoss 6.1, the deployed web services can be found in this screen:

http://localhost:8180/jbossws/services

you can click the endpoint address links of the services to view its wsdl. The StudentDAOImp service has this url for its wsdl:

http://localhost:8180/ProJee6/StudentDAOImp?wsdl

Here's the screen shot of JBoss web services screen:



  • 大小: 12.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics