JBOSS7 배포 서비스(一): EJB3 통합 Resteasy

7675 단어 jbossejbresteasy
1. local 인터페이스 클래스 만들기 IHEllo WorldLocal
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();

}

2. 인터페이스 구현 클래스 만들기 HelloWorldBean
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;
	}
}

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 + "]";
	}
}

3、web.xml 구성
<?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.IHEllo WorldLocal을 웹으로 복제합니다.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. 인터페이스 클래스(주의: 클래스를 실현하지 마), 웹.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. 만약에 jbjar가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
jb의 열배치를 진정으로 실현하지 못했습니다. 이것은 타이머 계승 ResteasyBootstrap을 단독으로 개발하여 실현해야 합니다

좋은 웹페이지 즐겨찾기