Imprimer 

Dans cet article, nous allons voir qu'un EJB stateless peut aussi être exposé en tant que web service.

Pour cet exemple, nous ferons deux projets maven :

 

Le projet Prj1WebServiceEjb

Le fichier pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.prj1</groupId>
<artifactId>prj1WebServiceEjb</artifactId>
<version>1.0</version>
<packaging>ejb</packaging>
<name>Prj1WebServiceEjb</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.2</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
</project>

Les classes java

L'objet métier :

package com.mycompany.prj1.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
*
* @author thierry
*/
@Entity
@Table(name = "PROJECT")

@NamedQueries({
@NamedQuery(name = Project.FIND_ALL_PROJECTS, query = "SELECT dto FROM Project dto"),
@NamedQuery(name = Project.FIND_BY_NAME, query = "SELECT dto FROM Project dto where upper(dto.name) like upper (:pName)")
})

public class Project {

public static final String FIND_ALL_PROJECTS = "PROJECT_FIND_ALL";

public static final String FIND_BY_NAME = "PROJECT_FIND_BY_NAME";

public Project() {
}

@Id
@Column(name = "PROJECT_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long projectId;

//@Column(name = "NAME", unique = true , nullable = false)
@Column(name = "NAME", unique = true, nullable = false)
private String name;

public Long getProjectId() {
return projectId;
}

public void setProjectId(Long projectId) {
this.projectId = projectId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return "Project : name=" + name;
}

}

L'interface métier :

package com.mycompany.prj1.inter;

import com.mycompany.prj1.beans.Project;
import java.util.List;

/**
*
* @author thierry
*/

public interface InterfaceProjectDao {

public Project insertProject(Project dto);

public List<Project> getAllProjects();

}

L'EJB accessible via un web service :

package com.mycompany.prj1.webservice;

import com.mycompany.prj1.beans.Project;
import com.mycompany.prj1.inter.InterfaceProjectDao;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.log4j.Logger;

/**
*
* @author thierry
*/
@WebService
@Stateless
public class ProjectDaoBeanEjb implements InterfaceProjectDao {

private static final Logger logger = Logger.getLogger(ProjectDaoBeanEjb.class);

@PersistenceContext(unitName = "puBdProject")
private EntityManager entityManager;

public Project insertProject(Project project) {

logger.info("Sauvegarde du projet : [" + project.getName() + "]");
if (project.getProjectId() == null) {
saveNewProject(project);
} else {
updateProject(project);
}
return project;
}

public List<Project> getAllProjects() {
List<Project> list = null;
try {
list = entityManager.createNamedQuery(Project.FIND_ALL_PROJECTS).getResultList();
} catch (Exception e) {
logger.error("Exception, message d'erreur : " + e.toString());
e.printStackTrace();
}
return list;
}

private void updateProject(Project project) {
logger.info("In updateProject");
entityManager.merge(project);
}

private void saveNewProject(Project project) {
logger.info("In saveNewProject");
entityManager.persist(project);
}

}

Le code source

Prj1WebServiceEjb.zip

Le projet Prj1WebServiceEjbClient

Le fichier pom.xml

Celui-ci génére les classes dont a besoin le client pour invoquer le web service :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.prj1</groupId>
<artifactId>Prj1WebServiceEjbClient</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Prj1WebServiceEjbClient</name>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<property name="target.dir" value="target" />
<delete dir="${target.dir}/classes/com/testapp/ws/client" />
<delete dir="${target.dir}/generated-sources/main/java/com/testapp/ws/client" />
<mkdir dir="${target.dir}/classes" />
<mkdir dir="${target.dir}/generated-sources/main/java" />
<taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
<classpath path="/home/thierry/glassfish-4.0/glassfish/modules/webservices-osgi.jar" />
<classpath path="/home/thierry/glassfish-4.0/glassfish/modules/jaxb-osgi.jar" />
<classpath path="/home/thierry/glassfish-4.0/glassfish/lib/javaee.jar" />
</taskdef>
<!-- ProjectDaoBeanService -->
<wsimport wsdl="http://localhost:8080/ProjectDaoBeanEjbService/ProjectDaoBeanEjb?wsdl"
destdir="${target.dir}/classes" verbose="true"
keep="true" sourceDestDir="${target.dir}/generated-sources/main/java" xendorsed="true" />
</tasks>
<sourceRoot>${project.build.directory}/generated-
sources/main/java</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.prj1.prj1testclientwebservice.TestAppelWebService</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>

La classe java d'appel du web service

package com.mycompany.prj1.prj1testclientwebservice;

import com.mycompany.prj1.webservice.Project;
import com.mycompany.prj1.webservice.ProjectDaoBeanEjb;
import com.mycompany.prj1.webservice.ProjectDaoBeanEjbService;
import java.util.List;
import javax.xml.ws.WebServiceRef;

/**
*
* @author thierry
*/
public class TestAppelWebServiceProject {

@WebServiceRef(wsdlLocation = "http://localhost:8080/ProjectDaoBeanEjbService/ProjectDaoBeanEjb?wsdl")
private static ProjectDaoBeanEjbService webService;

public static void main(String[] args) {

Project project = new Project();
project.setName("testPrj1");

ProjectDaoBeanEjb service = new ProjectDaoBeanEjbService().getProjectDaoBeanEjbPort();
List<Project> list = service.getAllProjects();
System.out.println("nombre de projet :" + list.size());

}

}

Code source

Prj1WebServiceEjbClient.zip