2016-04-11 10 views
0

JSONの無限ストリームを読み込む方法のページが多数見つかりました。私の質問は、スプリングブーツを使ってそれを生産する方法です。Springを使用してjsonの無限ストリームを送信する

実際、私は単純なjson Webサービスを制作しています。 これは私の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に行き、無限のデータストリームを見ることです。

私はJackson Stream APIについて読んだが、それはファイルにストリームするように見える...私はイベントについて読んでいるが、誰かがこの点で私を助けることができるなら.. ..

ありがとう!

+2

どのように無限のストリームを消費するのだろうか。無限(または無期限)の時間のデータを消費する場合、無限のストリームは必要ありません。さらに、WebSocketはおそらくそのタスクのためのより適切なツールでしょう。 – zeroflagL

+0

私はwebsocketを見ていて、私が必要とすることをするように見えます!有難うございます。それがうまくいくなら私はここにいくつかのコードを書くでしょう。 –

答えて

0

だから、これは私の新しいコントローラです:

@Controller 

パブリッククラスLocationStreamController {

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

}

これは私の設定ファイルである:

@Configuration 

@EnableWebSocketMessa geBroker パブリッククラスWebSocketConfigはAbstractWebSocketMessageBrokerConfigurer完璧に動作

{

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

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

}

を拡張します!あなたの答えをありがとう

関連する問題