001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.examples.core.oapi; 014 015import org.apache.juneau.examples.core.pojo.Pojo; 016import org.apache.juneau.httppart.HttpPartParser; 017import org.apache.juneau.httppart.HttpPartSchema; 018import org.apache.juneau.httppart.HttpPartSerializer; 019import org.apache.juneau.httppart.HttpPartType; 020import org.apache.juneau.oapi.OpenApiParser; 021import org.apache.juneau.oapi.OpenApiSerializer; 022 023/** 024 * Sample class which shows the simple usage of OpenApiSerializer. 025 * 026 * <ul class='seealso'> 027 * <li class='extlink'>{@source} 028 * </ul> 029 */ 030public class OapiExample { 031 032 033 /** 034 * Get a reference to a parser and usage of oapiserializer. 035 * 036 * @param args Unused. 037 * @throws Exception Unused. 038 */ 039 @SuppressWarnings("unused") 040 public static void main(String[] args) throws Exception{ 041 042 OpenApiSerializer oapiSerializer = OpenApiSerializer.DEFAULT; 043 044 OpenApiParser oapiParser = OpenApiParser.DEFAULT; 045 046 Pojo pojo = new Pojo("id","name"); 047 048 String flat = oapiSerializer.serialize(pojo); 049 // Print out the created POJO in OpenAPI format. 050 051 Pojo parse = oapiParser.parse(flat, Pojo.class); 052 053 assert parse.getId().equals(pojo.getId()); 054 assert parse.getName().equals(pojo.getName()); 055 056 //Http part schmea 057 HttpPartSchema schema = HttpPartSchema 058 .create("array") 059 .collectionFormat("pipes") 060 .items( 061 HttpPartSchema 062 .create("array") 063 .collectionFormat("csv") 064 .items( 065 HttpPartSchema.create("integer","int64") 066 ) 067 ) 068 .build(); 069 Object value = new long[][]{{1,2,3},{4,5,6},{7,8,9}}; 070 String output = OpenApiSerializer.DEFAULT.serialize(HttpPartType.HEADER, schema, value); 071 072 HttpPartSchema schemab = HttpPartSchema.create().type("string").build(); 073 // Convert POJO to BASE64-encoded string. 074 HttpPartSerializer s = OpenApiSerializer.DEFAULT; 075 String httpPart = s.serialize(schemab, pojo); 076 System.out.println(httpPart); 077 078 // Convert BASE64-encoded string back into a POJO. 079 HttpPartParser p = OpenApiParser.DEFAULT; 080 pojo = p.parse(schemab, httpPart, Pojo.class); 081 082 // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo 083 // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them. 084 } 085}