관리 메뉴

버리야 날자

[Jersey] Building Responses 본문

나만의 작업

[Jersey] Building Responses

버리 버리야 2009.07.21 10:44
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")
    public Response postEntity(String id) throws URISyntaxException{
     URI createdUri = new URI("http://localhost:8080/user/" + id);
     String created = create(id);
     return Response.created(createdUri).entity(created).build();
    }


    private String create(String content) {
System.out.println("post : " + content);
                //content create..
return content;
}

1. 201 상태코드 리턴하고 Location header에 값 넣어서 리턴하기.

$ curl -v -d "flyburi" http://localhost:8080/JerseyDemo/form/post

* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /JerseyDemo/form/post HTTP/1.1
> User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8k zlib/1.
2.3 libssh2/0.15-CVS
> Host: localhost:8080
> Accept: */*
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< Location: http://localhost:8080/user/flyburi
< Content-Length: 0
< Date: Mon, 20 Jul 2009 06:13:13 GMT
<
* Connection #0 to host localhost left intact
* Closing connection #0


2. Entity body에 custom response를 추가하는 방법.

$ curl -v -d "flyburi" http://localhost:8080/JerseyDemo/form/postEntity

* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /JerseyDemo/form/postEntity HTTP/1.1
> User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8k zlib/1.
2.3 libssh2/0.15-CVS
> Host: localhost:8080
> Accept: */*
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< Location: http://localhost:8080/user/flyburi
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Date: Mon, 20 Jul 2009 06:13:42 GMT
<
* Connection #0 to host localhost left intact
* Closing connection #0
flyburi
4 Comments
댓글쓰기 폼