2

moxy で継承を使用しようとしてスタック トレースが発生しています。誰かアドバイスをください。Java コードとスタック エラーの概要を以下に示します。前もって感謝します!!!

ビジネス オブジェクト

public abstract class ContactInfo {

}
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="address")
public class Address extends ContactInfo {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="phonenumber")
public class PhoneNumber extends ContactInfo {

}
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="customer")
public class Customer {

    private ContactInfo contactInfo;

    @XmlElementRef
    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }

}

cxf Web サービス

import javax.ws.rs.Consumes;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

import packagename.payment.ContactInfo;

import packagename.payment.Payment;

import packagename.payment.PaymentResponse;


@Path("/payment/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface PaymentServerRest {

    @POST
    @Path("aja1/")
    public PaymentResponse aja1(Customer customer);


}

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import packagename.payment.server.PaymentServerRest;

import packagename.payment.Address;

import packagename.payment.ContactInfo;

import packagename.payment.Customer;

import packagename.payment.PaymentResponse;

import packagename.payment.PhoneNumber;


@Path("/payment/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PaymentServerRestImpl implements PaymentServerRest {

    @Override
    @POST
    @Path("aja1/")
    public PaymentResponse aja1(Customer customer) {
        if (customer.getContactInfo() instanceof Address) {
            System.out.println("aja Address");
        } else if (customer.getContactInfo() instanceof PhoneNumber) {
            System.out.println("aja PhoneNumber");
        }
        return null;
    }

}

クライアント Web サービス

import packagename.payment.server.PaymentServerRest;
import packagename.payment.Customer;
import packagename.payment.Payment;
import packagename.payment.PaymentResponse;

public class PaymentClientRest  {

    private static PaymentServerRest server = createClientServer(PaymentServerRest.class, "paymentrest/");

    public static PaymentResponse aja1(Customer customer) {
        try {
            return server.aja1(customer);
        } catch (Exception e) {
            return null;
        }

    }


}

テストケース

import org.junit.Test;

import packagename.payment.Address;
import packagename.payment.Customer;
import packagename.payment.Payment;

public class PaymentClientRestTest {



    @Test
    public void testAja1() {
        Customer customer = new Customer();
        customer.setContactInfo(new Address());

        System.out.println(PaymentClientRest.aja1(customer));

        customer = new Customer();
        customer.setContactInfo(new PhoneNumber());

        System.out.println(PaymentClientRest.aja1(customer));


    }


}

春の設定beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <!-- <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /> -->
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
        <entry key="http://localhost:8080/servicepath/paymentrest" value="payments"/>
       </util:map>



    <bean id="jsonMoxyProvider" class="org.eclipse.persistence.jaxb.rs.MOXyJsonProvider">
        <property name="attributePrefix" value="@" />
        <property name="formattedOutput" value="true" />
        <property name="includeRoot" value="true" />
        <property name="marshalEmptyCollections" value="false" />
        <property name="valueWrapper" value="$" />
        <!-- property name="namespaceSeparator" value="" /--> <!-- by default is point -->
        <property name="namespacePrefixMapper" ref="jsonNamespaceMap" />
    </bean>

     <!-- Payment RestFul service -->
    <jaxrs:server id="paymentrest" address="/paymentrest">
        <jaxrs:serviceBeans>
            <ref bean="paymentServerRest" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonMoxyProvider"/>
        </jaxrs:providers>
    </jaxrs:server>

    <bean id="paymentServerRest"
        class="packagename.payment.server.impl.PaymentServerRestImpl" />



</beans>

例外

16:29:27.098 [Main Thread] ERROR o.a.cxf.jaxrs.client.AbstractClient - .Problem with writing the request message, class : class packagename.payment.Customer, ContentType : application/json.
org.apache.cxf.jaxrs.client.ClientWebApplicationException: org.apache.cxf.interceptor.Fault: .Problem with writing the request message, class : class packagename.payment.Customer, ContentType : application/json.
    at org.apache.cxf.jaxrs.client.AbstractClient.checkClientException(AbstractClient.java:501)
    at org.apache.cxf.jaxrs.client.AbstractClient.preProcessResult(AbstractClient.java:488)
    at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:531)
    at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:205)
    at $Proxy13.aja1(Unknown Source)
    packagename.payment.client.PaymentClientRest.aja1(PaymentClientRest.java:61)
    packagename.payment.client.PaymentClientRestTest.testAja1(PaymentClientRestTest.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:21)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:232)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:61)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:223)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:684)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.cxf.interceptor.Fault: .Problem with writing the request message, class : class packagename.payment.Customer, ContentType : application/json.
    at org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:649)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:526)
    ... 27 more
Caused by: org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with writing the request message, class : class packagename.payment.Customer, ContentType : application/json.
    at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:611)
    at org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:406)
    at org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:640)
    ... 29 more
**Caused by: javax.ws.rs.WebApplicationException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Invalid @XmlElementRef : Type "class packagename.payment.ContactInfo" or any of its subclasses are not known to this context.
    this problem is related to the following location:
        at public packagename.payment.ContactInfo packagename.payment.Customer.getContactInfo()
        at packagename.payment.Customer**

    at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.handleExceptionEnd(AbstractJAXBProvider.java:585)
    at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.handleJAXBException(AbstractJAXBProvider.java:597)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.writeTo(JSONProvider.java:361)
    at org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
    ... 30 more
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Invalid @XmlElementRef : Type "class packagename.payment.ContactInfo" or any of its subclasses are not known to this context.
    this problem is related to the following location:
        at public packagename.payment.ContactInfo packagename.payment.Customer.getContactInfo()
        at packagename.payment.Customer

    at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:466)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:298)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:141)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1163)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:203)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:363)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.getClassContext(AbstractJAXBProvider.java:425)
    at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.getJAXBContext(AbstractJAXBProvider.java:409)
    at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.createMarshaller(AbstractJAXBProvider.java:510)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.marshal(JSONProvider.java:508)
    at org.apache.cxf.jaxrs.provider.json.JSONProvider.writeTo(JSONProvider.java:357)
    ... 31 more
4

1 に答える 1

1

注: 私はEclipseLink JAXB(MOXy)のリーダーであり、JAXB(JSR-222)エキスパートグループのメンバーです。

私はあなたがこの問題を解決するのを手伝います。

JSONプロバイダー

最初の問題は、それがMOXyJsonProviderピックアップされていないように見え、CXFがデフォルトでデフォルトのJAXBベースのJSONバインディングになっていることです。これにより、JAXBRi例外がスローされます。

原因:javax.ws.rs.WebApplicationException:com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:1カウントのIllegalAnnotationExceptions無効な@XmlElementRef:「classpackagename.payment.ContactInfo」と入力するか、そのサブクラスのいずれかが不明ですこの文脈に。この問題は、次の場所に関連しています。public packagename.payment.ContactInfo packagename.payment.Customer.getContactInfo()at packagename.payment.Customer

at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.handleExceptionEnd(AbstractJAXBProvider.java:585)
at org.apache.cxf.jaxrs.provider.AbstractJAXBProvider.handleJAXBException(AbstractJAXBProvider.java:597)
at org.apache.cxf.jaxrs.provider.json.JSONProvider.writeTo(JSONProvider.java:361)
at org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
... 30 more

サブクラス

サブクラスについてMOXyに通知する必要があります。これを行う1つの方法は、@XmlSeeAlso注釈を使用することです。

@XmlSeeAlso({PhoneNumber.class, Address.class})
public abstract class ContactInfo {

}

アップデート

MOXyJsonProviderRESTEasyで実行しているときの問題を修正するために、EclipseLink2.4.2および2.5.0ストリームの修正をチェックインしました。これは、CXFで発生している問題に関連している場合と関連していない場合があります。この修正は、2013年2月22日以降、次の場所からナイトリービルドで利用できるようになります。

于 2013-02-21T11:44:03.060 に答える