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