JBoss AS 7内で実行されているアプリケーションで、私の(契約の最初の)RESTインターフェイス上にあるすべての着信XMLファイルを検証しようとしています。印刷(JBoss RESTeasyのドキュメントの例のように)と、unmarshallerのXMLスキーマの検証をオンに切り替えるための類似のものです。残念ながら、unmarshallerのデコレータは呼び出されません。ここでデコレータを使用したJBoss RESTeasy JAX-RS JAXBスキーマの検証
プリティ・デコレーターのためのコードです:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.xml.bind.Marshaller;
import org.jboss.resteasy.annotations.Decorator;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Decorator(processor = PrettyProcessor.class, target = Marshaller.class)
public @interface Pretty {}
と実装:今
import java.lang.annotation.Annotation;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import org.apache.log4j.Logger;
import org.jboss.resteasy.annotations.DecorateTypes;
import org.jboss.resteasy.spi.interception.DecoratorProcessor;
@DecorateTypes({ "text/*+xml", "application/*+xml", MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public class PrettyProcessor implements DecoratorProcessor<Marshaller, Pretty> {
private static final Logger LOGGER = Logger.getLogger(PrettyProcessor.class);
@Override
public Marshaller decorate(Marshaller target, Pretty annotation, Class type, Annotation[] annotations, MediaType mediaType) {
LOGGER.debug("Pretty.");
try {
target.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
} catch (PropertyException e) {
}
return target;
}
}
(動作しない)検証のための注釈:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.xml.bind.Unmarshaller;
import org.jboss.resteasy.annotations.Decorator;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Decorator(processor = ValidateProcessor.class, target = Unmarshaller.class)
public @interface Validate {}
実装:
import java.lang.annotation.Annotation;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import javax.xml.XMLConstants;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.log4j.Logger;
import org.jboss.resteasy.annotations.DecorateTypes;
import org.jboss.resteasy.spi.interception.DecoratorProcessor;
import org.xml.sax.SAXException;
@DecorateTypes({ "text/*+xml", "application/*+xml", MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public class ValidateProcessor implements DecoratorProcessor<Unmarshaller, Validate> {
private static final Logger LOGGER = Logger.getLogger(ValidateProcessor.class);
@Override
public Unmarshaller decorate(Unmarshaller target, Validate annotation, Class type, Annotation[] annotations, MediaType mediaType) {
target.setSchema(getSchema());
LOGGER.debug("Set validation schema.");
System.out.println("Set validation schema.");
return target;
}
}
とRESTインターフェースのコード:同じREST方式で
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.xml.ws.WebServiceException;
@Path("/{mdvt}/{ouid}/order")
public interface OrderResource {
@RolesAllowed({ "mpa" })
@POST
@Path("/update")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
@Produces(MediaType.APPLICATION_XML)
@Pretty
public Response update(@Context SecurityContext sec,
@PathParam("ouid") String ouID,
@PathParam("mdvt") long masterDataVersionTag,
@Validate UpdateOrdersRequest uor) throws WebServiceException;
}
、(更新)@Prettyデコレータ@Validateは呼び出されません。私はここで間違って何をしていますか?
古いバグDecorator for Jaxb unmarshaller doesn't workが見つかりました。
ここにあるコメントはすべてがうまくいっていると言っています。上記のコードは、ソリューションを含めてそこからのものです。それにもかかわらず、Unmarshallerには何も作用しません。
はい、私はその問題のための私の研究中にその解決策を見てきました。それでも、デコレータはうまくいくはずです。私がリンクしているバグレポートは、IMHOを再オープンする必要があります。 MessageBodyReaderを書くことは確かにうまくいくだろうが、私の目では私の問題には間違った解決策である。とにかく迅速な答えをありがとう! – Frank