使用Spring发送无限的json流

时间:2016-04-11 09:53:10

标签: java json spring spring-mvc jackson

我发现了很多关于如何阅读无限流JSON的页面。我的问题是如何用弹簧靴制作它?

实际上,我设法制作了一个简单的json网络服务。 这是我的pojo:

public class Location {

@Id
private long id;

private float x;
private float y;
private float z;
private String timestamp;

public Location(long id, float x, float y, float z, String timestamp) {
    super();
    this.id = id;
    this.x = x;
    this.y = y;
    this.z = z;
    this.timestamp = timestamp;
}

public Location(){
    super();
}

public Location(@JsonProperty("id")String id, @JsonProperty("x")String x,@JsonProperty("y") String y,
        @JsonProperty("z")String z, @JsonProperty("timestamp")String timestamp) {
    super();
    this.id = Long.parseLong(id);
    this.x = Float.parseFloat(x);
    this.y = Float.parseFloat(y);
    this.z = Float.parseFloat(z);
    this.timestamp = timestamp;
}

public long getId() {
    return id;
}

public float getX() {
    return x;
}

public float getY() {
    return y;
}

public float getZ() {
    return z;
}

public String getTimestamp() {
    return timestamp;
}

public void setId(long id) {
    this.id = id;
}

public void setX(float x) {
    this.x = x;
}

public void setY(float y) {
    this.y = y;
}

public void setZ(float z) {
    this.z = z;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

@Override
public String toString(){
    return "Location { id : " + id + " x : " + x +
            " y : " + y + " z : " + z + " }";
}

}

这是我的控制者:

@RestController 公共类LocationController {

private final RessourcesManager<Location> rm = new RessourcesManager<Location>();

/**
 * @param tagId tag that you want to get the position from
 * @return Location in json format
 */
@CrossOrigin
@RequestMapping("/getjson")
public Iterator<Location> location(@RequestParam(value="tagId", defaultValue="-1") String tagId){
    return rm.getAllElement();
}

@CrossOrigin
@RequestMapping(value="putjson", method = RequestMethod.POST)
  public @ResponseBody Location post( @RequestBody final  Location location) {    
      rm.addElement(location);
      return location;
  }

}

使用这段代码我可以生成并获取一个json对象。 但只是一个对象...... 我想要做的是继续myurl / getjson并看到无限的数据流。不仅是我的迭代器的返回..

我读到了关于杰克逊流API的内容,但看起来它只是流入一个文件......我正在阅读关于事件但是如果有人可以在这一点上帮助我......

谢谢!

1 个答案:

答案 0 :(得分:0)

所以这是我的新控制器:

@Controller
public class LocationStreamController {

    @CrossOrigin
    @MessageMapping("/json")
    @SendTo("/locations/location")
    public Location greeting(Location message) throws Exception {
        return message;
    }
}

这是我的配置文件:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/locations");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/json").setAllowedOrigins("*").withSockJS();
    }
}

完美的作品!谢谢你的回答