Object mappings for Rest-assured (objects Mapping)

Source: Internet
Author: User

Rest-assured supports mapping Java objects to JSON and XML, as well as mapping from JSON and XML to Java objects. JSON mapping requires that you have the Jackson, Jackson 2, or Gson,xml mappings in classpath to have JAXB in Classpath.

1.JsonPath Mapping

For example: Let's take a look at what we want to extract from the first winner to the JavaBean object:

JSON Data preparation:

1{"Lotto":{2"Lottoid": 5,3"Winning-numbers": [2,45,34,23,7,5,3],4"Winners":[{5"Winnerid": 23,6"Numbers": [2,45,34,23,3,5]7     },{8"Winnerid": 54,9"Numbers": [52,3,12,11,18,22]Ten     }] One   } A}

JavaBean object:

1  Public classWinner {2 3     Private intWinnerid;4     PrivateList<integer>numbers;5 6      Public intGetwinnerid () {7         returnWinnerid;8     }9 Ten      Public voidSetwinnerid (intWinnerid) { One          This. Winnerid =Winnerid; A     } -  -      PublicList<integer>getnumbers () { the         returnnumbers; -     } -  -      Public voidSetnumbers (list<integer>numbers) { +          This. Numbers =numbers; -     } +}

We can extract a winner as JavaBean:

1 Winner Winner = from (LOTTO). GetObject ("lotto.winners[0]", Winner.  Class);

Configuration:

You can configure Jsonpath by configuring custom object mappings, configuring predefined object mappings (already present), character sets, and so on. You can configure Jsonpath in the following ways:

1 New JsonPath (new File ("/tmp/document.json")). 2                            Using (new jsonpathconfig ("iso-8859-1")); 3 ..

When parsing /tmp/document.json文件的时候, notifies Jsonpath to display data in a content type=iso-8859-1 character set

2.xmlPath Object Mapping:

By using Xmlpath JAXB, you can easily map an XML file or do not divide an XML file into a Java object.

XML File Data preparation:

  1  <greetings> 2  <greeting > 3  <firstname>john</firstname> 4  <lastname>doe</lastname> 5  </greeting> 6  <greeting> 7  <firstname>jane</first Name> 8  <lastname>doe</lastname> 9  </greeting>10  <greeting>11  <firstname>some</firstname>12  <lastname>one</ Lastname>13  </greeting>14  </ Greetings> 

JavaBean Object Preparation:

1 @XmlRootElement2  Public classGreeting {3     PrivateString FirstName;4     PrivateString LastName;5 6      PublicString Getfirstname () {7         returnFirstName;8     }9 Ten      Public voidsetfirstname (String firstName) { One          This. FirstName =FirstName; A     } -  -      PublicString Getlastname () { the         returnLastName; -     } -  -      Public voidsetlastname (String lastName) { +          This. LastName =LastName; -     } +}

Map the first greeting in the XML document to the greeting instance:

1 Greeting greeting = from (greetingsxml). 2          GetObject ("greetings.greeting[0]", greeting.  Class);

Configuration:

You can configure Xmlpath by configuring custom object mappings, configuring predefined object mappings (already present), character sets, and so on. You can configure Xmlpath in the following ways:

1 New Xmlpath (new File ("/tmp/document.xml")). 2                         Using (new xmlpathconfig ("iso-8859-1")); 3 ..

Object mappings for Rest-assured (objects Mapping)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.