1、创建local接口类IHelloWorldLocal
package com.ejb.hello;
import javax.ejb.Local;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.ejb.hello.model.Book;
@Local
@Path("/hello")
@Consumes(MediaType.APPLICATION_JSON)
public interface IHelloWorldLocal {
@GET
@Path("/say/{s}")
@Produces({ MediaType.APPLICATION_JSON })
public String say( @PathParam("s") String s );
@GET
@Path("/say")
@Produces("application/json")
public List<Book> say();
}
package com.ejb.hello.impl;
import javax.ejb.Stateless;
import com.ejb.hello.IHelloWorldLocal;
import com.ejb.hello.model.Book;
@Stateless
public class HelloWorldBean implements IHelloWorldLocal {
@Override
public String say(String s) {
// TODO Auto-generated method stub
s = "Hello " + s;
System.out.println(s);
return s;
}
@Override
public List say() {
List list = new ArrayList();
Book vo = new Book();
vo.setIsbn("1");
vo.setTitle("jim");
list.add(vo);
vo = new Book();
vo.setIsbn("2");
vo.setTitle("tim");
list.add(vo);
return list;
}
}
vo类Book
package com.ejb.hello.model;
import java.io.Serializable;
public class Book implements Serializable {
private String isbn;
private String title;
public Book() {
}
public Book(String isbn, String title) {
this.isbn = isbn;
this.title = title;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
@Override
public String toString() {
return "Book [isbn=" + isbn + ", title=" + title + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_10" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>RestTest</display-name>
<context-param>
<param-name>resteasy.jndi.resources</param-name>
<param-value>java:global/ejb-hello/HelloWorldBean!com.ejb.hello.IHelloWorldLocal</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<!-- <context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param> -->
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
4、把上面的接口类和实现类打包生成jar
ant build代码片段
<property name="classes.dir" value="./web/WEB-INF/classes" />
<jar jarfile="ejb-hello.jar">
<fileset dir="./${classes.dir}">
<include name="**/IHelloWorldLocal.class" />
<include name="**/Book.class" />
<!--include name="**/jboss-deployment-structure.xml" /-->
</fileset>
</jar>
部署到jboss,jboss下打印出如下日志
13:47:27,976 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named HelloWorldBean in deployment unit deployment "ejb-hello.jar" are as follows:
java:global/ejb-hello/HelloWorldBean!com.ejb.hello.IHelloWorldLocal
java:app/ejb-hello/HelloWorldBean!com.ejb.hello.IHelloWorldLocal
java:module/HelloWorldBean!com.ejb.hello.IHelloWorldLocal
java:global/ejb-hello/HelloWorldBean
java:app/ejb-hello/HelloWorldBean
java:module/HelloWorldBean
说明,部署完成。
5、把日志中的java:global/ejb-hello/HelloWorldBean!com.ejb.hello.IHelloWorldLocal拷贝到web.xml,修改为
<context-param>
<param-name>resteasy.jndi.resources</param-name>
<param-value>java:global/ejb-hello/HelloWorldBean!com.ejb.hello.IHelloWorldLocal</param-value>
</context-param>
6、添加WEB-INF/jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
<deployment>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jaxrs" />
<module name="org.jboss.resteasy.resteasy-jaxb-provider" />
<!-- <module name="org.jboss.resteasy.resteasy-jettison-provider" /> -->
<!-- <module name="org.jboss.resteasy.resteasy-jackson-provider"/> -->
</dependencies>
</deployment>
</jboss-deployment-structure>
7、把接口类(注意:不要实现类)、web.xml和jsp等打包生成war,部署到jboss
ant build代码片段
<property name="war.name" value="RestTest.war" />
<jar jarfile="${war.name}">
<fileset dir="./web/">
<include name="**/IHelloWorldLocal.class" />
<include name="**/HelloWorldBean.class" />
<include name="**/Book.class" />
<include name="**/web.xml" />
<include name="WEB-INF/jboss-deployment-structure.xml" />
</fileset>
</jar>
8、测试
浏览器地址栏输入:http://localhost:8080/RestTest/rest/hello/say/earthhour
如果输出
Hello earthhour说明测试成功
输入:http://127.0.0.1:8080/RestTest/rest/hello/say
输出:
[{"isbn":"1","title":"jim"},{"isbn":"2","title":"tim"}]
9、也可以用代码测试
import org.jboss.resteasy.client.ProxyFactory;
import com.ejb.hello.IHelloWorldLocal;
public class ClientTestREST {
public static void main(String arg[]) {
// TODO Auto-generated method stub
try {
String REQUEST_PATH = "http://localhost:8080/RestTest/rest";
IHelloWorldLocal client = ProxyFactory.create(IHelloWorldLocal.class, REQUEST_PATH);
System.out.println(client.say("earthhour"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
10、如果ejb jar在war部署后,重新部署,不重新启动jboss情况下,再次测试时,会报错
Bad arguments passed to org.jboss.resteasy.spi.metadata.ResourceMethod@1f16d5e ( java.lang.String earthhour )
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
没有真正实现ejb的热部署,这就需要单独开发一个定时器继承ResteasyBootstrap来实现