RESTful Glassfish 4 애플리케이션 (JERSEY 2.22.2, JSON Provider로서 MOXY)에서 JSON 및 XML 출력을 모두 생성 할 수있는 리소스 메소드가 있습니다.미디어 유형 (JSON/XML)에 따라 다른 Jersey 2 응답
메소드 응답은 MessageBodyWriter를 통해 전달되지만 특정 경우에만 객체 그래프를 작성하는 데 사용됩니다. 이 경우 클라이언트의 요청한 미디어 유형과 독립적으로 그래프가 올바르게 적용됩니다.
반면 MessageBodyWirter의 isWriteable() 메서드가 false를 반환하면 MessageBodyFactory의 writers
목록에있는 다음 작성자에게 전달할 때 동작은 JSON 미디어 유형 요청과 XML 미디어간에 다릅니다 (요청 헤더에 각각 Accept: application/json
및 Accept: application/xml
).
첫 번째 경우 EntityFilteringFeature가 등록되어 있으므로 FilteringMoxyJsonProvider가 응답 작성자로 선택됩니다. 응답은 엔터티 필터링 주석을 기반으로 작성됩니다.
클라이언트가 XML 응답을 요청하면 다른 MessageBodyWriter (org.glassfish.jersey.jaxb.internal.XmlRootElementJaxbProvider)가 선택됩니다. XmlRootElementJaxbProvider 다음에 FilteringMoxyJsonProvider가 위치하는 MessageBodyFactory에서 WriterModel
이 순서화되어 있기 때문입니다.
이 상황에서 XML 응답은 필터를 적용하지 않고 작성됩니다.
우리는 작성자 순서를 변경하는 방법을 찾고 또한 행운없이 EntityFieldProcessor 클래스에 액세스하려고했습니다.
두 시나리오 (즉, JSON 및 XML 응답 요청)를 동일한 방식으로 처리 할 수 있습니까? MessageBodyFactory에서 일부 작성자를 등록하지 않거나 순서를 변경할 수 있습니까?
도움이 될 것입니다.
//Configuration
public class ApplicationConfigVersione1 extends ResourceConfig {
....
register(EntityFilteringFeature.class);
register(MyCustomWriter.class);
------------------------
@Produces({"application/json", "application/xml"})
public class MyCustomWriter implements MessageBodyWriter<MyCustomObject> {
....
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
if (mustUseCustomWriter()) {
return true;
} else {
return false;
//In this case, with request header accept=application/xml, the xml response is not filtered.
}
}
@Override
public void writeTo(MyCustomObject customObject, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
objectGraph = buildObjectGraph();
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, objectGraph);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.toString());
//**** objectGraph applies to XML and JSON media types
marshaller.marshall(object, entityStream);
동의,이 같은 문제의 :
나는 이것이 MOXY 구성 요소에 대한 실 거예요 - 수정 github의 문제를 우리가 필터링보고있는 행동의 원인이/폐쇄 생각한다. –