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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
eclipse-jee-2021-12-r-linux是一个针对Java EE开发的集成开发环境(IDE)。它是Eclipse IDE的一个版本,在2021年12月发布,并适用于Linux操作系统。 Eclipse-jee-2021-12-r-linux为Java EE开发者提供了一组丰富的工具和功能,以便于他们在Linux平台上进行Web和企业级应用程序的开发。它的特点包括: 1. Java EE支持:这个版本的Eclipse集成了Java EE的各种标准和技术,如JavaServer Faces(JSF)、JavaServer Pages(JSP)、Java Servlet、Enterprise JavaBeans(EJB)等。开发者可以方便地使用这些技术进行Web应用程序的开发。 2. 内置编辑器:Eclipse-jee-2021-12-r-linux提供了强大的代码编辑器,支持Java、HTML、CSS和JavaScript等语言的代码高亮和自动补全功能。编辑器还具有代码导航和重构等功能,方便开发者快速编写和修改代码。 3. 项目管理工具:这个版本的Eclipse集成了Maven和Gradle等流行的项目管理工具,开发者可以方便地创建和管理Java EE项目,并导入已有的项目。 4. 调试和测试:Eclipse-jee-2021-12-r-linux提供了强大的调试功能,开发者可以在开发过程中逐步调试代码,查看变量的值和堆栈跟踪等信息。此外,它还支持JUnit等测试框架,方便开发者编写和运行单元测试。 5. 版本控制集成:这个版本的Eclipse集成了常见的版本控制系统,如Git和Subversion,开发者可以方便地管理代码的版本和进行团队协作。 总之,eclipse-jee-2021-12-r-linux是一款功能强大的Java EE开发工具,适用于Linux操作系统。它通过提供丰富的工具和功能,简化了Java EE应用程序的开发过程,提高了开发效率,是Java EE开发者的首选工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值