Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to validate JSON with Moxy on marshaling/unmarshaling?
How to validate JSON with Moxy on marshaling/unmarshaling? [message #1828451] Wed, 10 June 2020 07:04
Yevgeny Lvovski is currently offline Yevgeny LvovskiFriend
Messages: 1
Registered: June 2020
Junior Member
We're using Payara 4 which has Moxy as the default mapper.

I'm trying to understand how I can validate json on unmarshaling/marshaling, based on the JAXB annotations the object have.

To validate **XML** on unmarshaling need to create and use a schema.
I generated schema for **Person** class:

Generated schema for Person:

   <?xml version="1.0" encoding="UTF-8"?>
    // I removed the schema xmlns line as  I cannot put in my first post links
       <xsd:complexType name="person">
          <xsd:sequence>
             <xsd:element name="firstName" type="xsd:string" minOccurs="0"/>
             <xsd:element name="lastName" type="xsd:string"/>
          </xsd:sequence>
       </xsd:complexType>
       <xsd:element name="Person" type="person"/>
    </xsd:schema>


**Person.class:**

   @XmlRootElement(name="Person")
    public class Person {
    
    	private String firstName;
    	private String lastName;
    	
    	
    	@XmlElement(name = "firstName")
    	public String getFirstName()
    	{
    		return firstName;
    	}
    	public void setFirstName(String firstName) 
    	{
    		this.firstName = firstName;
    	}
    	
    	@XmlElement(name = "lastName", required = true)
    	public String getLastName()
    	{
    		return lastName;
    	}
    	public void setLastName(String lastName) 
    	{
    		this.lastName = lastName;
    	}
    }


The code for unmarshaling:

JAXBContext jc = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource personXml = new StreamSource("Person.xml");	
    		
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Schema schema = sf.newSchema(new File("src/main/java/jaxb/PersonSchema1.xsd"));
    unmarshaller.setSchema(schema);	
    JAXBElement<Person> person = unmarshaller.unmarshal(personXml, Person.class);


This is working fine and will fail if Person.xml won't contain lastName attribute.
Now I'll change the code a little to unmarshal json:

 JAXBContext jc = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource personXml = new StreamSource("Person.json");
	
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Schema schema = sf.newSchema(new File("src/main/java/jaxb/PersonSchema1.xsd"));
    unmarshaller.setSchema(schema);	
    unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
    unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
    JAXBElement<Person> person = unmarshaller.unmarshal(personXml, Person.class);


In this case unmarshaling will fail on below exception even if Person.json has all the required attributes:

org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element ''.

Is it really the way to validate JSON?
Of course I would prefer to validate without any schema at all, but just to ask Moxy do the validation based on the annotations in Person.class
Previous Topic:ElementCollection List<String> Exception during deployment
Next Topic:Using Eclipselink with Streams in parallel gives NullPointerException
Goto Forum:
  


Current Time: Thu Apr 25 06:12:35 GMT 2024

Powered by FUDForum. Page generated in 0.03045 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top