Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spring-5-mvc/src/main/java/com/baeldung/Constants.java
Original file line number Diff line number Diff line change
@@ -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!";

}
2 changes: 0 additions & 2 deletions spring-5-mvc/src/main/java/com/baeldung/model/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public void setName(final String name) {
this.name = name;
}

//

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class DataSetupBean implements InitializingBean {
@Autowired
private FooRepository repo;

//

@Override
public void afterPropertiesSet() throws Exception {
IntStream.range(1, 5).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
86 changes: 86 additions & 0 deletions spring-5-mvc/src/main/webapp/WEB-INF/jsp/index.jsp
Original file line number Diff line number Diff line change
@@ -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>