2016-04-11 3 views
0

Ich habe viele Seiten gefunden, wie man einen unendlichen Stream von JSON lesen kann. Meine Frage ist, wie man es mit Federstiefeln herstellt?Mit Spring einen unendlichen Stream von JSON senden

Eigentlich habe ich es geschafft, eine einfache json Web-Services zu produzieren. Das ist mein 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 + " }"; 
} 

}

Und das ist mein Controller:

@RestController public class 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; 
    } 

}

diesen Code verwenden Ich kann generieren und bekommen JSON-Objekt. Aber nur ein Objekt ... Was ich tun möchte, ist auf Myurl/Getjson gehen und einen unendlichen Datenstrom zu sehen. Nicht nur die Rückkehr meines Iterators.

Ich lese über Jackson Stream API, aber es sieht aus wie es ist nur in eine Datei streamen ... Ich lese über Ereignis, aber wenn mir jemand in diesem Punkt helfen kann .. ..

Danke!

+2

Ich frage mich, wie Sie diesen unendlichen Strom verbrauchen würden. Das Verwenden von Daten für eine unendliche (oder unbestimmte) Menge an Zeit erfordert keinen unendlichen Strom. Außerdem wären Websockets wahrscheinlich ein geeigneteres Werkzeug für diese Aufgabe. – zeroflagL

+0

Ich schaue auf Websockets und es sieht aus, was ich brauche! Danke für das. Wenn es funktioniert, werde ich hier einen Code schreiben. –

Antwort

0

So das ist mein neuer Controller ist:

@Controller 

public class LocationStreamController {

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

}

Dies ist meine Config-Datei:

@Configuration 

@EnableWebSocketMessa geBroker public class WebSocketConfig erweitert AbstractWebSocketMessageBrokerConfigurer {

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

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

}

perfekt funktioniert! Vielen Dank für Ihre Antwort