Skip to content

Commit c51a976

Browse files
ThoughtscriptKevinGilmore
authored andcommitted
BAEL-1555 (eugenp#4652)
* BAEL-1555 * Corrected indents and spacing * RequestMapping to GetMapping
1 parent d48389c commit c51a976

File tree

7 files changed

+197
-4
lines changed

7 files changed

+197
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung;
2+
3+
public class Constants {
4+
5+
public static final String GENERIC_EXCEPTION = "Exception encountered!";
6+
7+
/**
8+
* API endpoints.
9+
*/
10+
public static final String API_RBE = "/rbe";
11+
public static final String API_SSE = "/sse";
12+
public static final String API_SRB = "/srb";
13+
14+
/**
15+
* API Responses.
16+
*/
17+
public static final String API_RBE_MSG = "I Was Sent From a Response Body Emitter!";
18+
public static final String API_SSE_MSG = "I Was Sent From a Sse!";
19+
public static final String API_SRB_MSG = "I Was Sent From a Streaming Response Body!";
20+
21+
}

spring-5-mvc/src/main/java/com/baeldung/model/Foo.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public void setName(final String name) {
4949
this.name = name;
5050
}
5151

52-
//
53-
5452
@Override
5553
public int hashCode() {
5654
final int prime = 31;

spring-5-mvc/src/main/java/com/baeldung/persistence/DataSetupBean.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public class DataSetupBean implements InitializingBean {
1616
@Autowired
1717
private FooRepository repo;
1818

19-
//
20-
2119
@Override
2220
public void afterPropertiesSet() throws Exception {
2321
IntStream.range(1, 5).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.web;
2+
3+
import com.baeldung.Constants;
4+
import java.util.Date;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
13+
14+
@Controller
15+
public class ResponseBodyEmitterController {
16+
17+
@GetMapping(Constants.API_RBE)
18+
public ResponseEntity<ResponseBodyEmitter> handleRbe() {
19+
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
20+
ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
21+
22+
nonBlockingService.execute(() -> {
23+
try {
24+
emitter.send(Constants.API_RBE_MSG + " @ " + new Date(), MediaType.TEXT_PLAIN);
25+
emitter.complete();
26+
} catch (Exception ex) {
27+
System.out.println(Constants.GENERIC_EXCEPTION);
28+
emitter.completeWithError(ex);
29+
}
30+
});
31+
32+
return new ResponseEntity(emitter, HttpStatus.OK);
33+
}
34+
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.web;
2+
3+
import com.baeldung.Constants;
4+
import java.util.Date;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
10+
11+
@Controller
12+
public class SseEmitterController {
13+
14+
@GetMapping(Constants.API_SSE)
15+
public SseEmitter handleSse() {
16+
SseEmitter emitter = new SseEmitter();
17+
18+
ExecutorService nonBlockingService = Executors.newSingleThreadExecutor();
19+
nonBlockingService.execute(() -> {
20+
try {
21+
emitter.send(Constants.API_SSE_MSG + " @ " + new Date());
22+
emitter.complete();
23+
} catch (Exception ex) {
24+
System.out.println(Constants.GENERIC_EXCEPTION);
25+
emitter.completeWithError(ex);
26+
}
27+
});
28+
29+
return emitter;
30+
}
31+
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.web;
2+
3+
import com.baeldung.Constants;
4+
import java.util.Date;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
10+
11+
@Controller
12+
public class StreamingResponseBodyController {
13+
14+
@GetMapping(Constants.API_SRB)
15+
public ResponseEntity<StreamingResponseBody> handleRbe() {
16+
StreamingResponseBody stream = out -> {
17+
String msg = Constants.API_SRB_MSG + " @ " + new Date();
18+
out.write(msg.getBytes());
19+
};
20+
return new ResponseEntity(stream, HttpStatus.OK);
21+
}
22+
23+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
2+
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
3+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
4+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
5+
6+
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
7+
8+
<!DOCTYPE html>
9+
<html lang="en">
10+
<head>
11+
<title>Spring MVC Async</title>
12+
<link href="<c:url value="/resources/styles/style.css"/>" rel="stylesheet">
13+
</head>
14+
<body>
15+
<main>
16+
<h2>Spring MVC Async</h2>
17+
<div id="rbe"></div>
18+
<div id="sse"></div>
19+
<div id="srb"></div>
20+
</main>
21+
</body>
22+
<script>
23+
24+
/**
25+
* AJAX Helpers.
26+
*/
27+
28+
var xhr = function(url) {
29+
return new Promise(function(resolve, reject) {
30+
try {
31+
var xmhr = new XMLHttpRequest();
32+
33+
//Listen for API Response
34+
xmhr.onreadystatechange = function() {
35+
if (xmhr.readyState == XMLHttpRequest.DONE && xmhr.status == 200) return resolve(xmhr.responseText);
36+
};
37+
38+
//Open connection
39+
xmhr.open("GET", url, true);
40+
//Additional headers as needed
41+
//x.withCredentials = true;
42+
//x.setRequestHeader("Accept", "application/json");
43+
//x.setRequestHeader("Content-Type", "text/plain");
44+
45+
//Perform the actual AJAX call
46+
xmhr.send();
47+
48+
} catch (ex) {
49+
reject("Exception: Oh CORS's you've made a mistake!");
50+
}
51+
});
52+
};
53+
54+
/**
55+
* RBE
56+
*/
57+
58+
xhr('http://localhost:8080/rbe').then(function(success){
59+
var el = document.getElementById('rbe');
60+
el.appendChild(document.createTextNode(success));
61+
el.appendChild(document.createElement('br'))
62+
});
63+
64+
/**
65+
* SSE
66+
*/
67+
68+
var sse = new EventSource('http://localhost:8080/sse');
69+
sse.onmessage = function (evt) {
70+
var el = document.getElementById('sse');
71+
el.appendChild(document.createTextNode(evt.data));
72+
el.appendChild(document.createElement('br'))
73+
};
74+
75+
/**
76+
* SRB
77+
*/
78+
79+
xhr('http://localhost:8080/srb').then(function(success){
80+
var el = document.getElementById('srb');
81+
el.appendChild(document.createTextNode(success));
82+
el.appendChild(document.createElement('br'))
83+
});
84+
85+
</script>
86+
</html>

0 commit comments

Comments
 (0)