BAEL-1555 (#4652)
* BAEL-1555 * Corrected indents and spacing * RequestMapping to GetMapping
This commit is contained in:
parent
d48389cbb6
commit
c51a97678c
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
|
||||||
|
public static final String GENERIC_EXCEPTION = "Exception encountered!";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API endpoints.
|
||||||
|
*/
|
||||||
|
public static final String API_RBE = "/rbe";
|
||||||
|
public static final String API_SSE = "/sse";
|
||||||
|
public static final String API_SRB = "/srb";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API Responses.
|
||||||
|
*/
|
||||||
|
public static final String API_RBE_MSG = "I Was Sent From a Response Body Emitter!";
|
||||||
|
public static final String API_SSE_MSG = "I Was Sent From a Sse!";
|
||||||
|
public static final String API_SRB_MSG = "I Was Sent From a Streaming Response Body!";
|
||||||
|
|
||||||
|
}
|
|
@ -49,8 +49,6 @@ public class Foo {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
|
|
@ -16,8 +16,6 @@ public class DataSetupBean implements InitializingBean {
|
||||||
@Autowired
|
@Autowired
|
||||||
private FooRepository repo;
|
private FooRepository repo;
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
IntStream.range(1, 5).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
|
IntStream.range(1, 5).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.web;
|
||||||
|
|
||||||
|
import com.baeldung.Constants;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class ResponseBodyEmitterController {
|
||||||
|
|
||||||
|
@GetMapping(Constants.API_RBE)
|
||||||
|
public ResponseEntity<ResponseBodyEmitter> handleRbe() {
|
||||||
|
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
|
||||||
|
ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
|
nonBlockingService.execute(() -> {
|
||||||
|
try {
|
||||||
|
emitter.send(Constants.API_RBE_MSG + " @ " + new Date(), MediaType.TEXT_PLAIN);
|
||||||
|
emitter.complete();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println(Constants.GENERIC_EXCEPTION);
|
||||||
|
emitter.completeWithError(ex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return new ResponseEntity(emitter, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.web;
|
||||||
|
|
||||||
|
import com.baeldung.Constants;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class SseEmitterController {
|
||||||
|
|
||||||
|
@GetMapping(Constants.API_SSE)
|
||||||
|
public SseEmitter handleSse() {
|
||||||
|
SseEmitter emitter = new SseEmitter();
|
||||||
|
|
||||||
|
ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
|
||||||
|
nonBlockingService.execute(() -> {
|
||||||
|
try {
|
||||||
|
emitter.send(Constants.API_SSE_MSG + " @ " + new Date());
|
||||||
|
emitter.complete();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println(Constants.GENERIC_EXCEPTION);
|
||||||
|
emitter.completeWithError(ex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return emitter;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.web;
|
||||||
|
|
||||||
|
import com.baeldung.Constants;
|
||||||
|
import java.util.Date;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class StreamingResponseBodyController {
|
||||||
|
|
||||||
|
@GetMapping(Constants.API_SRB)
|
||||||
|
public ResponseEntity<StreamingResponseBody> handleRbe() {
|
||||||
|
StreamingResponseBody stream = out -> {
|
||||||
|
String msg = Constants.API_SRB_MSG + " @ " + new Date();
|
||||||
|
out.write(msg.getBytes());
|
||||||
|
};
|
||||||
|
return new ResponseEntity(stream, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
|
||||||
|
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
|
||||||
|
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||||
|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
|
||||||
|
|
||||||
|
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>Spring MVC Async</title>
|
||||||
|
<link href="<c:url value="/resources/styles/style.css"/>" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main>
|
||||||
|
<h2>Spring MVC Async</h2>
|
||||||
|
<div id="rbe"></div>
|
||||||
|
<div id="sse"></div>
|
||||||
|
<div id="srb"></div>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AJAX Helpers.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var xhr = function(url) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
try {
|
||||||
|
var xmhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
//Listen for API Response
|
||||||
|
xmhr.onreadystatechange = function() {
|
||||||
|
if (xmhr.readyState == XMLHttpRequest.DONE && xmhr.status == 200) return resolve(xmhr.responseText);
|
||||||
|
};
|
||||||
|
|
||||||
|
//Open connection
|
||||||
|
xmhr.open("GET", url, true);
|
||||||
|
//Additional headers as needed
|
||||||
|
//x.withCredentials = true;
|
||||||
|
//x.setRequestHeader("Accept", "application/json");
|
||||||
|
//x.setRequestHeader("Content-Type", "text/plain");
|
||||||
|
|
||||||
|
//Perform the actual AJAX call
|
||||||
|
xmhr.send();
|
||||||
|
|
||||||
|
} catch (ex) {
|
||||||
|
reject("Exception: Oh CORS's you've made a mistake!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RBE
|
||||||
|
*/
|
||||||
|
|
||||||
|
xhr('http://localhost:8080/rbe').then(function(success){
|
||||||
|
var el = document.getElementById('rbe');
|
||||||
|
el.appendChild(document.createTextNode(success));
|
||||||
|
el.appendChild(document.createElement('br'))
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
var sse = new EventSource('http://localhost:8080/sse');
|
||||||
|
sse.onmessage = function (evt) {
|
||||||
|
var el = document.getElementById('sse');
|
||||||
|
el.appendChild(document.createTextNode(evt.data));
|
||||||
|
el.appendChild(document.createElement('br'))
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SRB
|
||||||
|
*/
|
||||||
|
|
||||||
|
xhr('http://localhost:8080/srb').then(function(success){
|
||||||
|
var el = document.getElementById('srb');
|
||||||
|
el.appendChild(document.createTextNode(success));
|
||||||
|
el.appendChild(document.createElement('br'))
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</html>
|
Loading…
Reference in New Issue