1. REST의 장점
- 언어, 플랫폼에 중립적임
- SOAP보단 개발 편이성이 좋음
- 학습 곡선이 작고, 개발 도구가 거의 필요없음
- 웹에 가까운 설계와 철학
2. REST의 단점
- point-to-point 통신 모델이라서 상호작용하는 분산환경에는 유용하지 않음
- 보안, 정책 등에 대한 표준이 없어 보안 대비해서 추가 구현사항 발생함
3. 아키텍처 트랜드
- Open API에서 REST 지원이 많이 이루어지고 있음(구글, 야휴, 트위터, facebook 등등)
- 쉬운 기술이 확산되기에는 유용함
4. Uniform Interface
- GET : 조회
- PUT : 새로운 자원을 생성, 기존 자원을 수정
- POST : 데이터 처리에 관련한 프로세스
- DELETE : 존재하는 리소스를 제거
- PUT과 POST의 차이 : PUT은 URI가 클라이언트에 의해 결정될 경우, POST는 서버에 의해 결정될 경우
5. REST 관련 오픈 소스들
- Apache CXF : http://cxf.apache.org/
- RestRet : http://www.restlet.org/
- Jersey : https://jersey.dev.java.net/
- 위 오픈 소스들을 활용해서 클라이언트나 서버 로직을 구현할 수 있음
6. JAX-RS 애노테이션
- @Path :실제 요청 URL을 나타낸다. 이 애노테이션은 클래스 선언부, 메서드 선언부 모두 위치할수 있으며 클래스 선언부에 @Path("/sample"), 메서드 선언부에 @Path("/xml") 이라고 설정한다면 해당 메서드의 호출은 /api/sample/xml 이 된다. 여기서 /api는 web.xml 파일에 설정된 Jersey URL이다.
- @GET : GET방식으로 데이터 추출, 선택가능한 옵션은 @PUT, @DELETE, @POST 등이 있다.
- @Consumes : 수신 하고자하는 데이터 포맷을 정의한다.
- @Produces : 출력하고자 하는 데이터 포맷을 정의한다. 여기서 사용하는 포맷은 "application/json", "text/xml" 이고 각각 json, xml 타입으로 데이터를 출력한다. 기타 선택가능한 옵션은 "application/atom+xml", "application/x-www-form-urlencoded", "application/octet-stream", "application/svg+xml", "application/xhtml+xml", "application/xml", "multipart/form-data", "text/html", "text/plain" 등이 있다.
- @QueryParam : URI 쿼리 파라미터의 값을 꺼내온다.
- @FormParam : Form값이 전송된 경우 Form안의 값들을 꺼내온다.
- @PathParam : URI template에 명시되어 있는 값을 꺼내온다.
- @CookieParam : 쿠키에 있는 값을 꺼내온다.
- @HeaderParam : 헤더에 있는 값을 꺼내온다.
- @Context : Request header나 URI 정도등등의 inject 정보를 사용할 수 있다.
- Produces와 Consumes 차이 : @Produces는 Accept 헤더를 참고하고 @Consumes은 Content-Type 헤더를 참고함
7. 예외 상황 처리
- WebApplication을 상속한 새로운 클래스인 NotFoundException을 생성하여 던지기
throw new NotFoundException("ID, " + id + ", is not found");
- 직접 WebApplicationException에 Response status로 바로 던지기
throw new WebApplicationException(Response.status.UNAUTHORIZED);
throw new WebApplicationException(400);
- ExceptionMapper 이용하는 예
@Provider
public class EntityNotFoundMapper implements
ExceptionMapper<javax.persistence.EntityNotFoundException>
{
public Response toResponse(
javax.persistence.EntityNotFoundException ex) {
return Response.status(404).
entity(ex.getMessage()).
type("text/plain").
build();
}
}
8. 클라이언트 샘플
public static void main(String[] args) {
ClientResponse response = null;
ClientConfig config = null;
WebResource service = null;
try {
config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
client.setConnectTimeout(7000);
client.setReadTimeout(10000);
service = client.resource(UriBuilder
.fromUri("http://localhost:8080/services/").build());
response = service.path("user/").path("4e4e34867cdd")
.queryParam("consumer_key", "test")
.queryParam("nonce", nonce).queryParam("signature", signature)
.get(ClientResponse.class);
System.out.println("status : " + response.getStatus());
System.out.println("Form response : "+response.getEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
출처: http://www.mimul.com/pebble/default/2010/05/01/1272710220000.html
'SPRING' 카테고리의 다른 글
[SpringBoot] 스프링부트로 웹서비스 출시(SpirngBoot & JPA) - 2[창천향로님 글 배우기] (0) | 2018.06.20 |
---|---|
[SPRING]AJAX에서 객체 필드 보내기 (0) | 2018.04.28 |
[JSP]C:forEach의 varStatus 사용(JSTL CORE) (0) | 2018.02.02 |
맵핑 객체 안에 리스트, 배열 바인딩 (0) | 2018.02.02 |
Form에서 전화번호 / 이메일 형식 만들기 (0) | 2018.02.02 |