jersey 11

[Jersey] WebApplicationException and Mapping Exception to Responses

이전에 블로깅했던 2009/06/05 - [나만의 작업] - Jersey의 Exception Handling 에 대해 조금 더 자세히 블로깅을 해봅니다. Jersey에서 Excpetion Handling하는 방법들 1) WebApplication을 상속한 새로운 클래스인 NotFoundException을 생성하여 던지기 NotFoundException을 throw하는 예 Example 1.25. Throwing Jersey specific exceptions to control response 1 @Path("items/{itemid}/") 2 public Item getItem(@PathParam("itemid") String itemid) { 3 Item i = getItems().get(itemid..

backend 2009.07.30

[Jersey] Building URIs

REST의 중요한 점 중의하나가 URIs이다. java.net.URI를 이용해서 URI를 만들고 쓰는 법은 쉽지 않다. 만약, baseUri로 "http://localhost:8080/base"가 있고, 여기에 상대경로를 다음과 같은 상대경로를 추가하고 싶다. "a:b;param=matrix/hello world" 그래서, "http://localhost:8080/base/a:b;param=matrix/hello%20world" 결과적으로 이런 URI가 되야 한다. java.net.URI를 이용해 이런 상대경로를 만들수가 없다. new URI("a:b;param=matrix/hello world"); new URI(null, null, "a:b;param=matrix/hello world", null);..

카테고리 없음 2009.07.24

[Jersey] Building Responses

Jersey에선 Response를 다음과 같이 할 수 있다. POST 메소드인 경우의 예를 들면, Jersey에선 POST는 작업을 수행한 후 정상적으로 처리되면 201 상태코드를 리턴하고 Location Header에는 새롭게 생성된 리소스의 URI값을 리턴할 수 있다. 이렇게 자바의 내용이 이렇다면, @POST @Path("post") public Response post(String id) throws URISyntaxException{ URI createdUri = new URI("http://localhost:8080/user/" + id); create(id); return Response.created(createdUri).build(); } @POST @Path("postEntity") p..

backend 2009.07.21

[Jersey] Representation and Java Types

참고 : Jersey 1.0.3 User Guide중의 내용 전에 올린 2009/06/02 - [나만의 작업] - JAX-RS @Produces와 @Consumes 에서 많이 본 Java type은 "String"인데, 거기서 본 String은 JAX-RS 구현을 지원하기 위해 필요한 많은 Java type들 중의 하나일뿐이다. Java type은 byte[], java.io.InputStream, java.io.Reader, java.io.File등이 있다. 게다가 JAXB beans도 지원해준다. 참고로 JAXB의 beans들은 JAXBElement나 @XmlRootElement 나 @XmlType라고 annotation 붙여진 클래스이다. Example 1.17. Using File with a s..

backend 2009.07.16

Jersey의 JSON Support

Jersey에서 JSON을 지원하는 방법은 두가지가 있다. * JAXB Based JSON support * Low-Level JSON support 일단, JAXB 기반의 JSON 지원하는 방법을 알아보면, JSON하고 XML data format을 쉽게 produce/consume하면 시간절약을 할 수 있다. 왜냐면 Java model, JAXB를 이용한 annotated된 POJO를 쓰기때문에 쉽게 핸들링할 수 있다. 자바 모델에 @XmlRootElement 어노테이션을 붙여주고 간단한 몇가지 작업을 해주면~ 큰 노력하지 않고 JSON을 지원할 수 있다. 단점은 매우 특별한 JSON format을 처리할 때 좀 어렵다는거.옵션을 줘야한다. 참고 : Jersey 1.0에서 RESTful 웹 서비스용 ..

backend 2009.06.08

Jersey의 Exception Handling

Jersey에서는 WebApplicationException 클래스를 이용하여 Exception Handling한다. WebApplicationException을 잡아야 하고, 예외를 Response로 매핑한다. 예외를 위한 response가 null이 아니면 응답을 생성, null이면 서버 오류 응답을 생성 런타임 예외나 미리감지되는 예외(checked exception) 기호에 따라 사용 미리 감지되지 않는 예외나 오류는 컨테이너 안쪽까지 전파가 되도록 예외를 다시 던져야(re-thrown) 한다. 미리 감지되는 예외나 throwable 은 직접 예외를 던지지 말고, 서블릿인 경우은 ServletException으로, JAX-WS Provider 기반인 경우는 WebServiceException으로 ..

backend 2009.06.05

Jersey의 Return Type

Jersey에서 사용할 수 있는 Return Type에는 void Response GenericEntity Java Type 네가지 타입이 있다. void : 204 status code (성공. message body가 empty) Response : null 리턴 값은 204 status code. GenericEntity / Other : 리턴값이 null이 아니면 200 status code를 사용하고 null이면 204 status code를 사용한다. Response의 instance에 어떤 metadata를 추가적으로 제공하고 싶으면 ResponseBuilder를 쓰면 된다. (ResponseBuilder는 빌더 패턴을 사용해 Response 인스턴스를 생성 한다.)

backend 2009.06.04

Jersey의 MessageBodyReader/Writer

JAX-RS는 MessageBodyReader와 MessageBodyWriter를 통해 HTTP 메세지 바디와 자바 타입간의 마샬링과 언마샬링을 해준다. MessageBodyReader의 생긴 모습을 보면, (이 인터페이스를 구현하여 원하는 모습의 자바타입 객체로 변신시킬 수 있다.) public interface MessageBodyReader { boolean isReadable(Class type, Type genericType, Annotation annotations[], MediaType mediaType); T readFrom(Class type, Type genericType, Annotation annotations[], MediaType mediaType, MultivaluedMap h..

backend 2009.06.03

JAX-RS @Produces와 @Consumes

* Content negotiation Representation을 서버에서 클라이언트로 받을 때 정보를 넘겨줄때 두가지 전략이 있다. (뷰를 판단하는 방법) 1. 하나는 다른 URI를 주는것. http://www.flyburi.com/user/buri.pdf http://www. flyburi.com/user/buri.xml 이렇게 다른 파일 확장자를 주는 방법 2. 다른 하나는 같은 URI를 주고 미디어타입의 리스트를 Accept HTTP request header에 주는 방법. http://www.flyburi.com/user/buri 라고 주고 Accept header에 application/pdf or text/xml이라고 주는 방법 이 방법을 content negotiation이라고 한다. A..

backend 2009.06.02

JAX-RS의 구성

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..

backend 2009.06.01

What is Jersey?

구글에서 "Jersey"를 치면 제일 처음으로 Map에서 저지섬이 나오고 두번째 링크로는 역시 위키피디아의 저지섬이 나온다. 세번째 링크가 되어서야 내가 원하는 jersey 공식 홈페이지가 나온다. Jersey는 SUN에서 개발하는 REST 방식의 웹 어플리케이션을 지원하는 JAX-RS의 구현체의 이름이다. Jersey is the open source (under dual CDDL+GPL license), production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. But, it is also more than the Reference Implementation. Jersey provides..

backend 2009.06.01