jee6 学习笔记 7.1 - @WebService

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:

[img]http://dl.iteye.com/upload/attachment/0071/8351/9ab72af9-9c1f-3afa-a55a-e731a7bfb9c7.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值