Apache Juneau基于 REST 构建的远程代理

联合创作 · 2023-10-01 10:38

Juneau

Apache Juneau 是Apache Software Foundation (ASF)开发的一款项目,由Apache Incubator PMC赞助。

关于

  • 使用通用框架将POJO编组到各种内容类型的工具包。

  • REST服务器API,通过使用POJO创建基于Swagger的Self-documenting REST接口。

  • 通过使用POJO与REST接口进行交互的REST客户端API。

  • 基于REST构建的远程代理API。

  • 一个复杂的INI配置文件API。

  • REST微服务API,结合了上述所有功能,用于创建以毫秒为单位启动的轻量级独立REST接口。

实例

核心库包括易于使用和可定制的序列化器和解析器。 Javadoc中提供了许多示例。默认序列化器通常可以用于在单行代码中的serializers POJOs :

// A simple POJO class
	public class Person {		public String name = "John Smith";		public int age = 21;
	}	
	// Serialize a bean to JSON, XML, or HTML
	Person p = new Person();	
	// Produces:
	// "{name:'John Smith',age:21}"
	String laxJson = JsonSerializer.DEFAULT_LAX.serialize(p);	
	// Produces:
	// "{"name":"John Smith","age":21}"
	String strictJson = JsonSerializer.DEFAULT.serialize(p);	
	// Produces:
	// <object>
	//   <name>John Smith</name>
	//   <age>21</age>
	// </object><
	String xml = XmlSerializer.DEFAULT_SIMPLE.serialize(p);	
	// Produces:
	// <table>
	//   <tr><td>name</td><td>John Smith</td></tr>
	//   <tr><td>age</td><td>21</td></tr>
	// </table>
	String html = HtmlSerializer.DEFAULT.serialize(p);	
	// Produces:
	// name=John+Smith&age=$n(21)
	String urlEncoding = UrlEncodingSerializer.DEFAULT.serialize(p);	// Produces:
	// $o(name=John Smith,age=$n(21))
	String uon = UonSerializer.DEFAULT.serialize(p);	// Produces:
	// 82 A4 name AA 4A John Smith 68 A3 age 15
	byte[] messagePack = MsgPackSerializer.DEFAULT.serialize(p);	// Produces:
	// <rdf:RDF
	//  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	//  xmlns:jp="http://www.ibm.com/juneaubp/"
	//  xmlns:j="http://www.ibm.com/juneau/">
	// 	<rdf:Description>
	// 		<jp:name>John Smith</jp:name>
	// 		<jp:age>21</jp:age>
	// 	</rdf:Description>
	// </rdf:RDF>
	String rdfXml = RdfSerializer.DEFAULT_XMLABBREV.serialize(p);	
	// Produces:
	// @prefix jp:      <http://www.ibm.com/juneaubp/> .
	// @prefix j:       <http://www.ibm.com/juneau/> .
	//	[]    jp:age  "21" ;
	//	      jp:name "John Smith" .
	String rdfN3 = RdfSerializer.DEFAULT_N3.serialize(p);	// Produces:
	// _:A3bf53c85X3aX157cf407e2dX3aXX2dX7ffd <http://www.ibm.com/juneaubp/name> "John Smith" .
	// _:A3bf53c85X3aX157cf407e2dX3aXX2dX7ffd <http://www.ibm.com/juneaubp/age> "21" .
	String rdfNTriple = RdfSerializer.DEFAULT_NTRIPLE.serialize(p);

解析POJO操作也很简单:

// Use one of the predefined parsers.
	ReaderParser parser = JsonParser.DEFAULT;
	
	// Parse a JSON object (creates a generic ObjectMap).
	String json = "{name:'John Smith',age:21}";
	Map m1 = parser.parse(json, Map.class);
	
	// Parse a JSON string.
	json = "'foobar'";
	String s2 = parser.parse(json, String.class);
	
	// Parse a JSON number as a Long or Float.
	json = "123";
	Long l3 = parser.parse(json, Long.class);
	Float f3 = parser.parse(json, Float.class);
	
	// Parse a JSON object as a bean.
	json = "{name:'John Smith',age:21}";
	Person p4 = parser.parse(json, Person.class);
	
	// Parse a JSON object as a HashMap<String,Person>.
	json = "{a:{name:'John Smith',age:21},b:{name:'Joe Smith',age:42}}";
	Map<String,Person> m5 = parser.parseMap(json, HashMap.class, String.class, Person.class)
	
	// Parse a JSON array of integers as a Collection of Integers or int[] array.
	json = "[1,2,3]";
	List<Integer> l6 = parser.parseCollection(json, LinkedList.class, Integer.class);
	int[] i6 = parser.parse(json, int[].class);
浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报