我很高兴地宣布我的第一个开源项目java-plist-serializer可以帮助您将Java(尤其是基于Spring的应用程序)与iOS应用程序集成。
背景
我正在将Java Webapp作为后端并且客户端是iOS设备的项目。
最近,我收到了创建Web服务的任务,该服务返回plist ( iOS中使用的Property List数据格式)作为响应。
为什么选择plist而不选择JSON或经典XML?
如果您必须针对iOS <5.0进行开发,则没有本机类可反序列化JSON。
核心iOS库支持“属性列表”格式,因此反序列化为NSDictionary超级高效。
很少有plist –与Java有关的库,但是每个库都需要做大量的手工工作,并将Java对象逐步重写为Apple NS *类的Java等效类。
我认为没有人喜欢这种任务。
那就是为什么我开发了一个库,用于以类似于XStream XML序列化的方式将Java对象序列化为Plist。
java-plist-serializer
java-plist-serializer是托管在Github上的开源项目,有助于开发Java应用程序和iOS应用程序之间的通信。
- 不依赖于任何与XML相关的库
- @PlistIgnore , @PlistAlias和命名策略可定制的输出
- 线程安全–可以在项目中用作单例
- 可扩展–可以轻松添加其他对象的处理程序
- PlistView提供的Spring Framework集成
用法
库的核心是PlistSerializerImpl 。 为了将对象序列化为plist,您必须创建PlistSerializerImpl的实例并调用序列化方法之一。 例如:
输入类别:
public class Post {
private String title;
private Integer views = 0;
private List<Comment> comments = new ArrayList<Comment>();
private Author author;
public Post(Author author, String title, Integer views) {
this.title = title;
this.views = views;
this.author = author;
}
}
public class Comment {
private String content;
private String author;
public Comment(String author, String content) {
this.content = content;
this.author = author;
}
}
public class Author {
private String name;
}
创建这些类的对象,并plistSerializer.toXmlPlist
方法
Post post = new Post(new Author("jason bourne"), "java-plist-serializer introduction", 9);
post.addComment(new Comment("maciejwalkowiak", "first comment"));
post.addComment(new Comment("john doe", "second comment"));
PlistSerializerImpl plistSerializer = new PlistSerializerImpl();
String xml = plistSerializer.toXmlPlist(post);
xml
变量将包含:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>author</key>
<dict>
<key>name</key>
<string>jason bourne</string>
</dict>
<key>comments</key>
<array>
<dict>
<key>author</key>
<string>maciejwalkowiak</string>
<key>content</key>
<string>first comment</string>
</dict>
<dict>
<key>author</key>
<string>john doe</string>
<key>content</key>
<string>second comment</string>
</dict>
</array>
<key>title</key>
<string>java-plist-serializer introduction</string>
<key>views</key>
<integer>9</integer>
</dict>
</plist>
Spring框架集成
为了返回plist作为Spring MVC控制器的响应,您可以使用扩展AbstractView的 PlistView 。
有几种方法可以配置Spring MVC。 最容易理解的PlistView用法示例:
@Controller
public class BlogController {
@RequestMapping(value = "/loadBlogPost", method = RequestMethod.GET)
public ModelAndView loadBlogPost() {
Post post = new Post(new Author("jason bourne"), "java-plist-serializer introduction", 9);
post.addComment(new Comment("maciejwalkowiak", "first comment"));
post.addComment(new Comment("john doe", "second comment"));
ModelMap model = new ModelMap();
model.addAttribute("RESULT", notification);
return new ModelAndView(new PlistView(), model);
}
}
更详细的文档可以在项目的github页面上找到
结论
随意叉,延伸。 如果您发现任何问题,请在github上报告。
参考: Java与iOS的对话:来自Java 伙伴JCG合作伙伴 Maciej Walkowiak(来自Software Development Journey博客)的Java对象与Apple plist序列化 。
翻译自: https://www.javacodegeeks.com/2012/07/java-talking-to-ios-java-objects-to.html