`

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
分享到:
评论

相关推荐

    jee6 学习笔记 6.3 - @Asynchronous

    在Java企业版(Java EE)6中,`@Asynchronous`注解是一个非常重要的特性,它使得开发者可以方便地在应用程序中实现异步处理。这个注解是Java EE并发编程的一部分,主要应用于EJB(Enterprise JavaBeans)环境,用于...

    开发工具 eclipse-jee-mars-2-win32

    开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32...

    eclipse-jee-mars-1-win32-x86_64.7z

    eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -&gt; General -&gt; Keys -&gt; Content Assist -&gt; Binding: 改为 Alt + / When:...

    eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-neon-1a-win_64

    eclipse-jee-neon-1a-win_64

    eclipse-jee-2022-06-R-win32-x86_64.zip

    在解压eclipse-jee-2022-06-R-win32-x86_64.zip后,我们会得到一个名为“eclipse”的文件夹,这个文件夹包含了整个IDE的所有组件和配置。启动Eclipse IDE,用户会看到熟悉的界面,包括工作区(Workspace)、透视图...

    eclipse-jee-2023-09-R-linux-gtk-x86-64.tar.gz

    "eclipse-jee-2023-09-R-linux-gtk-x86_64.tar.gz" 文件是Eclipse专为Java企业版(Java EE)开发者设计的2023年9月版本,适用于64位的Linux操作系统。这个版本包含了对Java EE开发所需的全部工具和功能,如Web服务器...

    eclipse-jee-2020-09-R-win32-x86_64.zip

    标题中的“eclipse-jee-2020-09-R-win32-x86_64.zip”指的是Eclipse IDE for Java EE Developers的2020年9月版本,适用于Windows 32位和64位系统的安装包。Eclipse是一款著名的开源集成开发环境(IDE),广泛用于...

    eclipse-jee-2021-12-R-win32-x86_64.zip

    Eclipse IDE for Enterprise Java and Web Developers (eclipse-jee-2021-12-R-win32-x86_64.zip)适用于Windwos x86_64

    eclipse-jee-2020-12-R-win32-x86_64

    《Eclipse IDE for Java开发者:深入解析eclipse-jee-2020-12-R-win32-x86_64》 Eclipse IDE,全称集成开发环境(Integrated Development Environment),是全球广泛使用的开源Java开发工具。该版本"eclipse-jee-...

    eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip

    Eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip是一个针对Windows平台的64位版本的Eclipse集成开发环境(IDE)的压缩包,特别为Java企业级(J2EE)应用程序开发设计。该版本发布于2015年6月30日,是当时Eclipse ...

    jee6 学习笔记 5 - Struggling with JSF2 binding GET params

    这篇"jee6 学习笔记 5 - Struggling with JSF2 binding GET params"主要探讨了开发者在使用JSF2绑定GET参数时可能遇到的挑战和解决方案。 JSF2是一个基于MVC(模型-视图-控制器)设计模式的Java框架,用于创建交互...

    eclipse-jee-2023-09-R-macosx-cocoa-x86-64.dmg

    eclipse-jee-2023-09-R-macosx-cocoa-x86_64.dmg 适用于macOS Intel芯片系统

    eclipse-jee-2023-06-R-win32-x86-64.zip

    《Eclipse JEE 2023-06-R for Windows x64:深度解析与应用》 Eclipse JEE 2023-06-R-win32-x86_64.zip 是...通过不断学习和熟练掌握Eclipse JEE的各项功能,你可以更高效地构建、测试和部署高质量的Java EE应用程序。

    eclipse-jee-2018-09-win32-x86_64.zip

    标题 "eclipse-jee-2018-09-win32-x86_64.zip" 提供的信息表明这是一款针对Java企业级开发的Eclipse集成开发环境(IDE)的2018年9月版本,适用于Windows 32位操作系统、x86_64架构的计算机。Eclipse是一个开源的、跨...

    jee6 学习笔记 1 - 开发环境的配置

    NULL 博文链接:https://jxee.iteye.com/blog/1575432

    eclipse-jee-2019-03-R-win32-x86-64.zip

    Eclipse-JEE-2019-03-R-win32-x86-64.zip 是一个专门为Windows 64位系统设计的Eclipse版本,包含了Web开发所需的插件。 这个版本是2019年的第三个发布版(Release),通常每个版本都会带来性能优化、新功能和修复...

    eclipse-jee-2023-09-R-win32-x86-64.zip

    "eclipse-jee-2023-09-R-win32-x86_64.zip" 是Eclipse针对2023年9月发布的一个更新版,专为Windows 64位操作系统设计。 在Windows系统上安装和使用Eclipse-JEE,你需要了解以下关键知识点: 1. **安装步骤**: - ...

    eclipse-jee-mars-2-win32

    eclipse-jee-mars-2-win32 javaee开发工具 eclipse-jee-mars-2-win32 javaee开发工具

    eclipse-jee-oxygen-3-win32.rar

    "eclipse-jee-oxygen-3-win32.rar" 是一个针对Windows 32位系统的Eclipse版本,专为Java企业级开发(JEE)设计。这个版本是Eclipse Oxygen系列的第三个发布版,它包含了众多开发者所需的工具和特性,旨在提升开发...

Global site tag (gtag.js) - Google Analytics