JAX-RS의 구성

Posted at 2009/06/01 13:34// Posted in 나만의 작업
1. Root Resource Classes

* 웹 리소스을 구현하기 위해 JAX-RS 어노테이션을 사용하는 자바 클래스
 적어도 하나의 메소드에 @Path을 사용한 POJO

Root resource class들은 JAX-RS runtime에 인스턴스화된다.
 @Path 어노테이션이 달린 Resource 클래스

@Path("/hi")
public class HiResource {
    @GET 
    @Produces("text/plain")
    public String getAsText() {
        return "Hi! buri. Show Text.";
    }


2. Resource Methods

@GET
@POST
@PUT
@DELETE
@HEAD

*  URI Templates

@Path annotation의 값은 상대 경로 URI.

@Path("user list/{id}")
@Path("user%20list/{id})

두개의  path는 동등하다. 어노테이션의 값은 자동으로 인코딩된다

정규 표현식도 가능하다.

* sub resources
리소스 클래스에서 @Path 어노테이션이 달린 메소드는 하위 리소스 메소드나 하위 리소스 로케이터가 된다.

3. Extracting Request Parameters

@FormParam : Form값이 전송된 경우 Form안의 값들을 꺼내온다.
@QueryParam : URI 쿼리 파라미터의 값을 꺼내온다.
@PathParam : URI template에 명시되어 있는 값을 꺼내온다.  
@CookieParam : 쿠키에 있는 값을 꺼내온다.
@HeaderParam : 헤더에 있는 값을 꺼내온다.
@Context : Request header나  URI 정도등등의 inject 정보를 사용할 수 있다.

@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("name") String name){ .... }


@GET
@Path("/user/{id}")
public void get(@PathParam("id") String id) { .... }


@GET
public String get(@Context UriInfo ui){
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    MultivaluedMap<String, String> pathParams = ui.getPathParameters();
}


@GET
public String get(@Context HttpHeaders hh){
    MultivaluedMap<String, String> headerParams = ui.getRequestHeaders();
    Map<String, Cookie> pathParams = ui.getCookies();
}


* Annotation Inheritance

public interface ReadOnlyAtomFeed {  
   @GET @Produces("application/atom+xml")  
   Feed getFeed();  
 }  
  
 @Path("feed")  
 public class ActivityLog implements ReadOnlyAtomFeed {  
    public Feed getFeed() {...}  
 } 

* 기타 Annotation

@DefaultValue : @Context만 빼고 위의 다른 어노테이션에서 기본값 설정을 할때에 사용

@Encoded : @FormParam, @MatrixParam, @QueryParam, @PathParam 에서 파라미터 값을 자동으로 URI 디코딩하지 않도록 한다.



'나만의 작업' 카테고리의 다른 글

Jersey의 Exception Handling  (0) 2009/06/05
Jersey의 Return Type  (0) 2009/06/04
Jersey의 MessageBodyReader/Writer  (0) 2009/06/03
JAX-RS @Produces와 @Consumes  (2) 2009/06/02
JAX-RS의 구성  (0) 2009/06/01
What is Jersey?  (0) 2009/06/01
What is JAX-RS?  (0) 2009/05/29
What is REST?  (2) 2009/05/27
Thinkfree Office Live 한국어 서비스 시작  (2) 2009/04/01

댓글을 남겨주세요

Name *

Password *

Link (Your Homepage or Blog)

Comment

Secret

[Spring] @Autowired의 Before/After

Posted at 2009/04/29 15:35// Posted in 나만의 작업/Spring

Spring framework 2.5에 추가된 @Autowired annotation에 관한 글을 보고 정리.

알고있는 내용이기에~ 그냥 가볍게 Before 와 After code

Before - @Autowired annotation이 없었을 때 

applicationContext.xml에서 설정
<bean id="empDao" class="EmpDao" /> <bean id="empManager" class="EmpManager"> <property name="empDao" ref="empDao" /> </bean>

EmpDao의 bean을 inject
public class EmpManager { private EmpDao empDao; public EmpDao getEmpDao() { return empDao; } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } ... }

이랬던 코드가~ 바뀐다. 

After

applicationContext.xml에서 설정

<context:annotation-config /> <!-- 요거 꼭 빼먹지 말것 --> <bean id="empManager" class="autowiredexample.EmpManager" /> <bean id="empDao" class="autowiredexample.EmpDao" />

import org.springframework.beans.factory.annotation.Autowired; public class EmpManager { @Autowired private EmpDao empDao; }

Annotation을 써서 훨씬 보기에 간결한 코드를 짤 수 있다.









  1. [NC]...YellOw
    2009/04/29 22:13 [Edit/Del] [Reply]
    인자 저도 스프링 공부해야하는데 요기서 많은 도움을 얻을 수 있을거 같네요!!

댓글을 남겨주세요

Name *

Password *

Link (Your Homepage or Blog)

Comment

Secret

8. Annotation 기반으로 JUnit4를 이용한 Spring TDD

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback=true)

@RunWith : 테스트를 실행할 org.junit.runner.Runner 구현 클래스를 지정할 수 있다.

스프링은 스프링 컨텍스트 설정 및 DI, 트랜잭션 처리 등을 지원해주는 Runner 구현 클래스를 제공하고 있다.

Junit 4 기반의 테스트에서 스프링 컨텍스트에 설정된 빈 객체를 사용하고 싶다면 SpringJunit4ClassRunner 클래스를 @RunWith 어노테이션 값으로 설정하면 된다.

@ContextConfiguration : 스프링 컨텍스트를 생성할 때 사용될 설정 파일의 경로

@Autowired, @Resource : 테스트 코드에서 필요로 하는 스프링 빈 객체 설정


  • 트랜잭션 처리를 위한 설정

@TransactionConfiguration : 테스트 클래스에 적용되며, 트랜잭션 관리자와 기본 롤백 규칙을 설정한다.

@Rollback : 테스트 메소드에 적용되며, 메소드 단위로 롤백 규칙을 설정한다.

@NotTransactional : 트랜잭션을 적용하지 않을 메소드에 적용한다.

@Transactional : 테스트 클래스에 적용. 이 어노테이션을 적용함으로써 각 테스트 메소드는 트랜잭션 범위 내에서 실행된다.

만약 테스트 메소드에서 명시적으로 롤백 여부를 설정하고 싶다면 @Rollback 어노테이션을 사용하면 된다.

@Rollback(false)

테스트 메소드에 대한 트랜잭션이 시작되기 전에 어떤 초기화 작업을 해주어야 한다며, @BeforeTransaction 어노테이션이 적용된 메소드를 작성하면 된다.

반대로 트랜잭션이 종료된 이후에 정리 작업을 해주어야 할 경우에는 @AfterTransaction 어노테이션이 적용된 메소드를 작성하면 된다.



@RunWith (SpringJUnit4ClassRunner. class )

@ContextConfiguration (locations = { "classpath:/com/mydomain/data/beans.xml" })

@TransactionConfiguration (transactionManager = "transactionManager" ,defaultRollback= true )

@Transactional

public class iBatisDaoTest {

 

          @Autowired

          private ImageService imageService ;

          private int id ;

 

          @Test

          public void testInsertImage() {}

}



9. Reference

 

SQL Maps2.0 개발자가이드 ( 한글판 )
http://ibatis.apache.org/docs/java/pdf/iBATIS-SqlMaps-2_ko.pdf

SQL Maps2.0 tutorial( 한글판 )
http://ibatis.apache.org/docs/java/pdf/iBATIS-SqlMaps-2-Tutorial_ko.pdf

iBATIS 에서   생성되는  SQL 문을   보기   위한  log4j 셋팅
http://openframework.or.kr/JSPWiki/Wiki.jsp?page=IbatisLog4jSettingToShowSQL
 
Sql2ibatis
http://openframework.or.kr/JSPWiki/Wiki.jsp?page=Sql2ibatis

DDL2iBatis
http://openframework.or.kr/JSPWiki/attach/Hibernate/DDL2iBatis-exe.zip

Spring 2.5, Eclipse and JUnit 4.4
http://dertompson.com/index.php/2007/12/12/spring-25-eclipse-and-junit-44/

iBATIS 액션: 쉽고 강력한 SQL 매핑 프레임워크 아이바티스
- 클린턴 비긴.브랜든 구딘.래리 메도스 지음 | 이동국.손권남 번역,  위키북스

스프링 2.5 프로그래밍
- 최범균 , 가메출판사

 


  1. 2008/09/03 13:50 [Edit/Del] [Reply]
    맨날 모르는 이야기만 나온다 ㅠ_ㅠ
  2. seattle
    2008/09/04 09:10 [Edit/Del] [Reply]
    잘봤습니다. 다음엔 동영상 강의도 제작해주세요!

댓글을 남겨주세요

Name *

Password *

Link (Your Homepage or Blog)

Comment

Secret