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 static org.apache.juneau.httppart.HttpPartType.*; 016 017import org.apache.juneau.examples.core.pojo.*; 018import org.apache.juneau.httppart.*; 019import org.apache.juneau.oapi.*; 020 021/** 022 * Sample class which shows the simple usage of OpenApiSerializer. 023 * 024 * <ul class='seealso'> 025 * <li class='extlink'>{@source} 026 * </ul> 027 */ 028public class OapiExample { 029 030 031 /** 032 * Get a reference to a parser and usage of oapiserializer. 033 * 034 * @param args Unused. 035 * @throws Exception Unused. 036 */ 037 @SuppressWarnings("unused") 038 public static void main(String[] args) throws Exception{ 039 040 OpenApiSerializer oapiSerializer = OpenApiSerializer.DEFAULT; 041 042 OpenApiParser oapiParser = OpenApiParser.DEFAULT; 043 044 Pojo pojo = new Pojo("id","name"); 045 046 String flat = oapiSerializer.serialize(pojo); 047 // Print out the created POJO in OpenAPI format. 048 049 Pojo parse = oapiParser.parse(flat, Pojo.class); 050 051 assert parse.getId().equals(pojo.getId()); 052 assert parse.getName().equals(pojo.getName()); 053 054 //Http part schmea 055 HttpPartSchema schema = HttpPartSchema 056 .create("array") 057 .collectionFormat("pipes") 058 .items( 059 HttpPartSchema 060 .create("array") 061 .collectionFormat("csv") 062 .items( 063 HttpPartSchema.create("integer","int64") 064 ) 065 ) 066 .build(); 067 Object value = new long[][]{{1,2,3},{4,5,6},{7,8,9}}; 068 String output = OpenApiSerializer.DEFAULT.serialize(HEADER, schema, value); 069 070 HttpPartSchema schemab = HttpPartSchema.create().type("string").build(); 071 // Convert POJO to BASE64-encoded string. 072 HttpPartSerializer s = OpenApiSerializer.DEFAULT; 073 String httpPart = s.createPartSession(null).serialize(HEADER, schemab, pojo); 074 System.out.println(httpPart); 075 076 // Convert BASE64-encoded string back into a POJO. 077 HttpPartParser p = OpenApiParser.DEFAULT; 078 pojo = p.createPartSession(null).parse(HEADER, schemab, httpPart, Pojo.class); 079 080 // The object above can be parsed thanks to the @Beanc(properties = id,name) annotation on Pojo 081 // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them. 082 } 083}