2012年12月19日水曜日

RESTEasy を使ったバリデーション(続編)

この前ご紹介した、RESTEasy の Bean Validator を使ってみようと思ったら、こんなエラーが。orz


22:36:21,356 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/rest].[jp.vyw.rest.sample.SampleApplication]] (http--127.0.0.1-8080-1) サーブレット jp.vyw.rest.sample.SampleApplication のServlet.service()が例外を投げました: org.jboss.resteasy.spi.UnhandledException: javax.validation.ConstraintDeclarationException: Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints, but there are parameter constraints defined at all of the following overridden methods: [MethodMetaData [method=public abstract void jp.vyw.rest.sample.api.ISample.insert(java.lang.String,java.lang.String), parameterMetaData=[ParameterMetaData [type=class java.lang.String], [index=0], name=arg0], constraints=[NotNull, Size], isCascading=false], ParameterMetaData [type=class java.lang.String], [index=1], name=arg1], constraints=[], isCascading=false]], constraints=[], isCascading=false, hasParameterConstraints=true], MethodMetaData [method=public void jp.vyw.rest.sample.api.ISample$$$view33.insert(java.lang.String,java.lang.String), parameterMetaData=[ParameterMetaData [type=class java.lang.String], [index=0], name=arg0], constraints=[NotNull, Size], isCascading=false], ParameterMetaData [type=class java.lang.String], [index=1], name=arg1], constraints=[], isCascading=false]], constraints=[], isCascading=false, hasParameterConstraints=true]]
        at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:245) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
        at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
        at java.lang.Thread.run(Thread.java:679) [rt.jar:1.6.0_24]
Caused by: javax.validation.ConstraintDeclarationException: Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints, but there are parameter constraints defined at all of the following overridden methods: [MethodMetaData [method=public abstract void jp.vyw.rest.sample.api.ISample.insert(java.lang.String,java.lang.String), parameterMetaData=[ParameterMetaData [type=class java.lang.String], [index=0], name=arg0], constraints=[NotNull, Size], isCascading=false], ParameterMetaData [type=class java.lang.String], [index=1], name=arg1], constraints=[], isCascading=false]], constraints=[], isCascading=false, hasParameterConstraints=true], MethodMetaData [method=public void jp.vyw.rest.sample.api.ISample$$$view33.insert(java.lang.String,java.lang.String), parameterMetaData=[ParameterMetaData [type=class java.lang.String], [index=0], name=arg0], constraints=[NotNull, Size], isCascading=false], ParameterMetaData [type=class java.lang.String], [index=1], name=arg1], constraints=[], isCascading=false]], constraints=[], isCascading=false, hasParameterConstraints=true]]
        at org.hibernate.validator.metadata.AggregatedMethodMetaData$Builder.checkParameterConstraints(AggregatedMethodMetaData.java:222) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.metadata.AggregatedMethodMetaData$Builder.build(AggregatedMethodMetaData.java:161) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.metadata.BeanMetaDataImpl.buildMethodMetaData(BeanMetaDataImpl.java:391) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.metadata.BeanMetaDataImpl.<init>(BeanMetaDataImpl.java:286) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.metadata.BeanMetaDataImpl.<init>(BeanMetaDataImpl.java:163) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.engine.ValidatorImpl.getBeanMetaData(ValidatorImpl.java:1211) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:302) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:139) [hibernate-validator-4.2.0.Final.jar:4.2.0.Final]
        at org.jboss.resteasy.plugins.validation.hibernate.HibernateValidatorAdapter.applyValidation(HibernateValidatorAdapter.java:39) [resteasy-hibernatevalidator-provider-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:150) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.2.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.2.Final.jar:]
        ... 20 more
いやーん・・・・・。

詳しくまとまってるサイトを見つけたですが、関連は不明だけど、こんなことが書いてある。
To provide translations to other languages, one must create a new ValidationMessages_XX.properties file with the translated messages, where XX is the code of the language being provided.

Unfortunately Hibernate Validator provider doesn’t supports I18N based on a specific HTTP request. It does not take Accept-Language HTTP header into account and always uses the default Locale as provided by Locale.getDefault(). To be able to change the Locale using the Accept-Language HTTP header, a custom implementation must be provided.

んー、もうちょっと頑張ってみる。

5 件のコメント:

  1. この件は、EJB を使うと生じるみたい。POJO で REST サービス作る分には、Bean Validator がちゃんと動く。

    返信削除
  2. これ関係あるのかなぁ・・・
    https://hibernate.onjira.com/browse/HV-624?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

    返信削除
  3. REST サービスから、@Stateless 外して他の EJB を呼ぶようにしたらできるのはできる。以下と同じ状況。
    ただし、@EJB がこれまた使えない。どうなってんのよ。
    http://stackoverflow.com/questions/10331041/resteasy-ejb-returning-null
    https://community.jboss.org/thread/170194

    InitialContext#lookup() なら当たり前だけど大丈夫。

    返信削除
  4. 本件、ようやく解決。
    beans.xml を置いとけば、@EJB だけでインジェクションできますよ。っていう CDI サマサマな結果でした。

    返信削除
  5. でも、REST API 自体に、@Stateless 書くと同じエラーが出るので、注意><

    返信削除