我有一个对象是“级别”对象的列表,我正在测试用 Spring Boot Rest Controller 以两种方式传输它们:
使用 JSON,在 Rest Controller 中我使用类似:
@RequestMapping(value = "/api/v1/layers/{layername}", method = RequestMethod.GET, produces = "application/json") public @ResponseBody List<Level> query(@PathVariable String layername, @RequestParam("northEastLat") Float northEastLat, @RequestParam("northEastLng") Float northEastLng, @RequestParam("northWestLat") Float northWestLat, @RequestParam("northWestLng") Float northWestLng, @RequestParam("southEastLat") Float southEastLat, @RequestParam("southEastLng") Float southEastLng, @RequestParam("southWestLat") Float southWestLat, @RequestParam("southWestLng") Float southWestLng ) { List<Level> poligons=levelService.obtainLevels(layername,southWestLng,southWestLat,northWestLng,northWestLat,northEastLng,northEastLat,southEastLng,southEastLat); int i=1; for (Level p : poligons) { System.out.println("poligon" + i++ + " is:" + p.toString()); } return poligons; }
使用 Protostuff Protobuf 格式,我使用类似:
@RequestMapping(value = "/api/v1/layers/{layername}", method = RequestMethod.GET,produces = "text/plain") public String query(@PathVariable String layername, @RequestParam("northEastLat") Float northEastLat, @RequestParam("northEastLng") Float northEastLng, @RequestParam("northWestLat") Float northWestLat, @RequestParam("northWestLng") Float northWestLng, @RequestParam("southEastLat") Float southEastLat, @RequestParam("southEastLng") Float southEastLng, @RequestParam("southWestLat") Float southWestLat, @RequestParam("southWestLng") Float southWestLng ) { List<Level> poligons=levelService.obtainLevels(layername,southWestLng,southWestLat,northWestLng,northWestLat,northEastLng,northEastLat,southEastLng,southEastLat); LevelList list = new LevelList(poligons); byte[] bytes; int i=1; for (Level p : poligons) { System.out.println("poligon" + i++ + " is:" + p.toString()); } Schema<LevelList> schema = RuntimeSchema.getSchema(LevelList.class); LinkedBuffer buffer = LinkedBuffer.allocate(); try { bytes = ProtostuffIOUtil.toByteArray(list, schema, buffer); } finally { buffer.clear(); } return new String(bytes); }
关卡对象格式为:[{"wkb_geometry":"{"type":"Polygon","coordinates":[[[24.446822,45.34997],[24.706508,45.352485]]]}","id":199, “级别”:“3”,“类型”:null}
级别对象是:
@Entity(name = "Level")
@Table(name="Level2G")
@SecondaryTables({
@SecondaryTable(name="Level3G"),
@SecondaryTable(name="Level4G")
})
public class Level implements Serializable {
private static final long serialVersionUID = 1L;
// @Column(name = "wkb_geometry",columnDefinition="Geometry")
//@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name="wkb_geometry")
private /*Geometry */ String wkb_geometry;
@Id
@Column(name="id")
private Integer id;
@Column(name="level")
private String level;
@Transient
private String type;
public Level() {
}
public Level(String wkb_geometry, Integer id, String level) {
this.wkb_geometry = wkb_geometry;
this.id = id;
this.level = level;
this.type = "Feature";
}
public Level(String wkb_geometry, Integer id, String level, String type) {
this.wkb_geometry = wkb_geometry;
this.id = id;
this.level = level;
this.type = type;
}
public Object getWkb_geometry() {
return wkb_geometry;
}
public void setWkb_geometry(String wkb_geometry) {
this.wkb_geometry = wkb_geometry;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Level{" +
"wkb_geometry=" + wkb_geometry +
", id=" + id +
", level='" + level + '\'' +
", type='" + type + '\'' +
'}';
}
}
LevelList 对象只是 Level 对象的列表
问题在于,与 JSON (3.7kb) 相比,使用 Protostuff 我得到了更大的有效负载 (26 kb)。为什么?
同样对于第二个选项,我还尝试将“application/octet-stream”设置为直接返回字节,但结果仍然相同。我还比较了 JSON 和 protobuf 的速度;即使负载更大,protobuf 也具有更好的性能。知道为什么吗?