users@jersey.java.net

Re: [Jersey] Error Marshalling Object without _at_XmlRootElement

From: Felipe Gaścho <fgaucho_at_gmail.com>
Date: Fri, 14 Aug 2009 21:07:04 +0200

if you can change the xsd:


<xsd:element name="AddressType">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="AddressBaseType">
<xsd:sequence>
<xsd:element ref="UserArea" minOccurs="0"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

then you have your RootElement :)

otherwise you can wrapp your type creating the JAXBElement at runntime:

        @GET
        @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        @Path("read/{id}")
        public JAXBElement<FpAbstractUser> read(@PathParam("id") String id) {
                return new JAXBElement<FpAbstractUser>(FpAbstractUser.QUALIFIED_NAME,
                                FpAbstractUser.class, abstractUserFacade.read(Integer
                                                .valueOf(id)));
        }


On Fri, Aug 14, 2009 at 5:44 PM, Steven Ford<stevefordtn_at_gmail.com> wrote:
> I have created classes from the OAGIS 9.4 xsd's using JAXB. I am writing a
> RESTful service using Jersey that consumes & produces an AddressType.
> Basically I validate the address and put stuff in the userArea.  The
> AddressType class does not have an @XmlRootElement annotation and I want to
> use the Class as it was generated by JAXB, so I don't want to manually add
> it. The XML is successful transformed into an AddressType object coming in
> but I get the following error when returning the AddressType:
> A message body writer for Java type, class
> org.openapplications.oagis._9.AddressType, and MIME media type,
> application/xml, was not found
> My question is: How can I have my service produce AddressType xml
> I have seen in some messages where it says to return a JAXBElement
> generatated from the ObjectFactory. The ObjectFactory that was generated by
> JAXB returns AddressType, not JAXBElement
> from ObjectFactory:
>     /**
>      * Create an instance of {_at_link AddressType }
>      *
>      */
>     public AddressType createAddressType() {
>         return new AddressType();
>     }
>
>
> Here is my code:
> @POST
> @Path("/validate")
> @Consumes( "application/xml" )
> @Produces( "application/xml" )
> public AddressType validateAddress(AddressType address) {
> String city = address.getCityName().getValue();
> String state = address.getCountrySubDivisionCode().get(0).getValue();
> String zipcode = address.getPostalCode().getValue();
>
> AddressMatch validationAddress = AddressValidatorDao.validateAddress(city,
> state, zipcode);
>
> UserAreaType userArea = address.getUserArea();
> int matchLevel = Integer.parseInt(validationAddress.getMatchLevel());
> if(matchLevel != 0) {
> userArea.getAny().add(validationAddress);
> }else {
> List<AddressMatch> addressList =
> AddressValidatorDao.getCityStateByZipcode(zipcode);
> if(!addressList.isEmpty()) {
> userArea.getAny().add(addressList);
> }else {
> addressList = AddressValidatorDao.getZipcodeByCityState(city, state);
> if(!addressList.isEmpty()) {
> userArea.getAny().add(addressList);
> }else {
> userArea.getAny().add(validationAddress); //Put the original No Match Found
> }
> }
> }
>
> return address;
> }
> XSD snippet:
> <xsd:complexType name="AddressBaseType" abstract="true">
> <xsd:annotation>
> <xsd:documentation
> source="http://www.openapplications.org/oagis/9"></xsd:documentation>
> </xsd:annotation>
> <xsd:sequence>
> <xsd:element ref="ID" minOccurs="0" maxOccurs="unbounded"/>
> <xsd:element ref="FormatCode" minOccurs="0"/>
> <xsd:element ref="AttentionOfName" minOccurs="0" maxOccurs="unbounded"/>
> <xsd:element ref="CareOfName" minOccurs="0" maxOccurs="unbounded"/>
> <xsd:choice>
> <xsd:element ref="AddressLine" minOccurs="0" maxOccurs="unbounded"/>
> <xsd:sequence>
> <xsd:element ref="LineOne" minOccurs="0"/>
> <xsd:element ref="LineTwo" minOccurs="0"/>
> <xsd:element ref="LineThree" minOccurs="0"/>
> <xsd:element ref="LineFour" minOccurs="0"/>
> <xsd:element ref="LineFive" minOccurs="0"/>
> </xsd:sequence>
> <xsd:sequence>
> <xsd:element ref="BuildingNumber" minOccurs="0"/>
> <xsd:element ref="BuildingName" minOccurs="0"/>
> <xsd:element ref="StreetName" minOccurs="0"/>
> <xsd:element ref="Unit" minOccurs="0"/>
> <xsd:element ref="Floor" minOccurs="0"/>
> <xsd:element ref="PostOfficeBox" minOccurs="0"/>
> <xsd:element ref="DeliveryPointCode" minOccurs="0" maxOccurs="unbounded"/>
> </xsd:sequence>
> </xsd:choice>
> <xsd:element ref="CitySubDivisionName" minOccurs="0" maxOccurs="unbounded"/>
> <xsd:element ref="CityName" minOccurs="0"/>
> <xsd:element ref="CountrySubDivisionCode" minOccurs="0"
> maxOccurs="unbounded"/>
> <xsd:element ref="CountryCode" minOccurs="0"/>
> <xsd:element ref="PostalCode" minOccurs="0"/>
> <xsd:element ref="Status" minOccurs="0"/>
> <xsd:element ref="Preference" minOccurs="0"/>
> </xsd:sequence>
> <xsd:attribute name="languageCode" type="LanguageCodeContentType"
> use="optional"/>
> <xsd:attribute name="type" type="NormalizedStringType"/>
> </xsd:complexType>
> <xsd:complexType name="AddressType">
> <xsd:complexContent>
> <xsd:extension base="AddressBaseType">
> <xsd:sequence>
> <xsd:element ref="UserArea" minOccurs="0"/>
> </xsd:sequence>
> </xsd:extension>
> </xsd:complexContent>
> </xsd:complexType>
> The AddressType Class generated by JAXB:
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "AddressType", propOrder = {
>     "userArea"
> })
> public class AddressType
>     extends AddressBaseType
> {
>     @XmlElement(name = "UserArea")
>     protected UserAreaType userArea;
>     /**
>      * Gets the value of the userArea property.
>      *
>      * @return
>      *     possible object is
>      *     {_at_link UserAreaType }
>      *
>      */
>     public UserAreaType getUserArea() {
>         return userArea;
>     }
>     /**
>      * Sets the value of the userArea property.
>      *
>      * @param value
>      *     allowed object is
>      *     {_at_link UserAreaType }
>      *
>      */
>     public void setUserArea(UserAreaType value) {
>         this.userArea = value;
>     }
> }
>
>



-- 
Looking for a client application for this service:
http://fgaucho.dyndns.org:8080/footprint-service/wadl