2017-01-23 18 views
0

私はSpring WebSocket exampleを研究しています。 db < - > server < - > clientから情報を交換するようなアプリケーションを作成したいと思います。私は自分のbeanを作成してdbに問い合わせを行い、この場合はAnimalBeanです。ここではアプリケーションのコントローラは、次のとおりです。Spring WebSocketはサーバーから複数のペア値メッセージを送信します

@Controller 
public class GreetingController { 

    @MessageMapping("/hello") 
    @SendTo("/topic/greetings") 
    public Greeting greeting(HelloMessage message, AnimalBean ab) throws Exception { 
     return new Greeting(ab.getCows() + "\t" + new Date() + "\t" + message.getName()); 
    } 

} 

私はたとえばので、1つのJSONメッセージにそれを送信することが可能である場合、私は思っていたクライアントにab.getCows()またはab.getRabbits()などのような動物の異なるカウントを送信したいのでメッセージは次のようになります。

{"cows":"4", "rabbits":"60"}

は、それが達成されるCANDとそれを行うための最も簡単な方法は何ですか?

答えて

1

あなたのDAO BeanはAnimalBeanです。 Updatedクラスは次のようになります。

@Controller 
public class GreetingController { 

    @Autowired 
    private AnimalBean ab; 

    @MessageMapping("/hello") 
    @SendTo("/topic/greetings") 
    public AnimalInfogreeting(HelloMessage message) throws Exception { 
     return new AnimalInfo(ab.getCows(), ab.getRabbits()); 
    } 

} 

POJOクラスを作成します。

public class AnimalInfo{ 
    private int cows; 
    pirvate int rabbits; 

    public AnimalInfo(int cows, int rabbits){ 
     this.cows= cows; 
     this.rabbits =rabbits; 
    } 

    //getters and setters 
} 
関連する問題