[BAEL-8473] - Moved articles from spring-boot module to new spring-boot-client module
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.boot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public static void main(String[] args) {
|
||||
applicationContext = SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.baeldung.boot.client;
|
||||
|
||||
public class Details {
|
||||
|
||||
private String name;
|
||||
|
||||
private String login;
|
||||
|
||||
public Details() {
|
||||
}
|
||||
|
||||
public Details(String name, String login) {
|
||||
this.name = name;
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.baeldung.boot.client;
|
||||
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class DetailsServiceClient {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public DetailsServiceClient(RestTemplateBuilder restTemplateBuilder) {
|
||||
restTemplate = restTemplateBuilder.build();
|
||||
}
|
||||
|
||||
public Details getUserDetails(String name) {
|
||||
return restTemplate.getForObject("/{name}/details", Details.class, name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.baeldung.websocket.client;
|
||||
|
||||
public class Message {
|
||||
|
||||
private String from;
|
||||
private String text;
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package org.baeldung.websocket.client;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.messaging.simp.stomp.StompCommand;
|
||||
import org.springframework.messaging.simp.stomp.StompHeaders;
|
||||
import org.springframework.messaging.simp.stomp.StompSession;
|
||||
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* This class is an implementation for <code>StompSessionHandlerAdapter</code>.
|
||||
* Once a connection is established, We subscribe to /topic/messages and
|
||||
* send a sample message to server.
|
||||
*
|
||||
* @author Kalyan
|
||||
*
|
||||
*/
|
||||
public class MyStompSessionHandler extends StompSessionHandlerAdapter {
|
||||
|
||||
private Logger logger = LogManager.getLogger(MyStompSessionHandler.class);
|
||||
|
||||
@Override
|
||||
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
|
||||
logger.info("New session established : " + session.getSessionId());
|
||||
session.subscribe("/topic/messages", this);
|
||||
logger.info("Subscribed to /topic/messages");
|
||||
session.send("/app/chat", getSampleMessage());
|
||||
logger.info("Message sent to websocket server");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
|
||||
logger.error("Got an exception", exception);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getPayloadType(StompHeaders headers) {
|
||||
return Message.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleFrame(StompHeaders headers, Object payload) {
|
||||
Message msg = (Message) payload;
|
||||
logger.info("Received : " + msg.getText() + " from : " + msg.getFrom());
|
||||
}
|
||||
|
||||
/**
|
||||
* A sample message instance.
|
||||
* @return instance of <code>Message</code>
|
||||
*/
|
||||
private Message getSampleMessage() {
|
||||
Message msg = new Message();
|
||||
msg.setFrom("Nicky");
|
||||
msg.setText("Howdy!!");
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.baeldung.websocket.client;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
|
||||
import org.springframework.messaging.simp.stomp.StompSessionHandler;
|
||||
import org.springframework.web.socket.client.WebSocketClient;
|
||||
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
||||
import org.springframework.web.socket.messaging.WebSocketStompClient;
|
||||
|
||||
/**
|
||||
* Stand alone WebSocketStompClient.
|
||||
*
|
||||
*/
|
||||
public class StompClient {
|
||||
|
||||
private static String URL = "ws://localhost:8080/spring-mvc-java/chat";
|
||||
|
||||
public static void main(String[] args) {
|
||||
WebSocketClient client = new StandardWebSocketClient();
|
||||
WebSocketStompClient stompClient = new WebSocketStompClient(client);
|
||||
|
||||
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
|
||||
|
||||
StompSessionHandler sessionHandler = new MyStompSessionHandler();
|
||||
stompClient.connect(URL, sessionHandler);
|
||||
|
||||
new Scanner(System.in).nextLine(); // Don't close immediately.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user