2012-09-14 4 views
0

나는 api verioning을 실험하고 있으며 이에 반대하는 매우 독특한 요구 사항이 있습니다. 우리는 콘텐츠 협상 즉, @Produces annotation을 사용하려고합니다. @Produces ({ "th/v1-v10 + xml"})와 같은 형식으로 사용자 정의 미디어 유형을 원합니다. 여기서 v1-v10은이 API "th/v1 + xml", "th/v2 + xml"의 Accept 헤더와 함께 "th/v10 + xml"까지의 모든 요청을 처리합니다.@ 제작자/제공자의 미디어 유형이 일치합니다

나는 조금 이상하다고 생각하지만, 우리가 생산에서 떨어 뜨리는 것은 각자 새로운 버전의 클라이언트가 될 것이지만, 모든 서비스가 수정되는 것은 아닙니다. 따라서 범위가있는 서비스에 주석을 달아서 변경하지 않아도 모든 드롭에 대해 복제 할 필요가 없습니다.

그래서 @Path 및 @Produces 주석과 일치하는 동안 저지에서 로그인을 가로 챌 수있는 방법이 있습니까? 나는 미디어 유형에 맞는 정규식을 사용할 수 없다는 것을 알고있다.

.......

좀 더 연구는 뉴저지는 요청 헤더와 서비스 제공 업체 미디어 타입을 받아 들일 사이의 호환성을 결정하는 MediaType.isCompatible (MediaType에 다른) 메서드를 호출 하더군요.

사용자 지정 MediaType을 만들고 isCompatible 메서드를 재정의 할 수있는 경우이 기능을 약간 활용할 수 있습니다. Jersey는 그러한 확장을 허용합니까 ??

도움을 주시면 감사하겠습니다.

답변

0

맞춤 응답 맵퍼를 사용해야합니다.

1 .- 응답

@Provider 
public class MyResponseTypeMapper 
    implements MessageBodyWriter<MyResponseObjectType> { 
    @Override 
    public boolean isWriteable(final Class<?> type,final Type genericType, 
       final Annotation[] annotations, 
       final MediaType mediaType) { 
     ... use one of the arguments (either the type, an annotation or the MediaType) 
      to guess if the object shoud be written with this class 
    } 
    @Override 
    public long getSize(final MyResponseObjectType myObjectTypeInstance, 
        final Class<?> type,final Type genericType, 
         final Annotation[] annotations, 
        final MediaType mediaType) { 
     // return the exact response length if you know it... -1 otherwise 
     return -1; 
    } 
    @Override 
    public void writeTo(final MyResponseObjectType myObjectTypeInstance, 
        final Class<?> type,final Type genericType, 
        final Annotation[] annotations, 
        final MediaType mediaType, 
        final MultivaluedMap<String,Object> httpHeaders, 
        final OutputStream entityStream) throws IOException,                 WebApplicationException { 
     ... serialize/marshall the MyResponseObjectType instance using 
      whatever you like (jaxb, etC) 
     entityStream.write(serializedObj.getBytes()); 
    } 
} 

2 .- 앱에서 매퍼 등록을 서면으로 담당 MessageBodyWriter를 구현하는 클래스를 만듭니다

public class MyRESTApp 
    extends Application { 
    @Override 
public Set<Class<?>> getClasses() { 
     Set<Class<?>> s = new HashSet<Class<?>>(); 
     s.add(MyResponseTypeMapper.class); 
     return s; 
    } 
} 

저지 호출 등록 된 모든 매퍼를 검색합니다 그들의 isWriteable() 메소드가 true를 반환 할 때까지이 MessageBodyWriter 인스턴스를 사용하여 클라이언트에 내용을 serialize합니다.

관련 문제