mirror of
https://github.com/Febbweiss/springboot-react-webpack.git
synced 2026-03-05 06:35:36 +00:00
Refactoring: project and java package
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package fr.pavnay.demo.springboot.react;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||
|
||||
//@EnableOAuth2Sso
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletContextTemplateResolver templateResolver() {
|
||||
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
|
||||
resolver.setPrefix("/templates/");
|
||||
resolver.setSuffix(".html");
|
||||
resolver.setTemplateMode("LEGACYHTML5");
|
||||
resolver.setCacheable(false);
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.pavnay.demo.springboot.react.domain;
|
||||
|
||||
public class Comment {
|
||||
|
||||
private final Long id;
|
||||
private final String author;
|
||||
private final String text;
|
||||
|
||||
public Comment(Long id, String author, String text) {
|
||||
this.id = id;
|
||||
this.author = author;
|
||||
this.text = text;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package fr.pavnay.demo.springboot.react.rest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import fr.pavnay.demo.springboot.react.domain.Comment;
|
||||
import fr.pavnay.demo.springboot.react.service.CommentService;
|
||||
|
||||
@RestController
|
||||
public class CommentController {
|
||||
|
||||
@Autowired
|
||||
private CommentService service;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String index() {
|
||||
return "Greetings from Spring Boot!";
|
||||
}
|
||||
|
||||
@RequestMapping(value="/api/comments", method=RequestMethod.GET)
|
||||
public @ResponseBody List<Comment> comments() {
|
||||
return service.getAll();
|
||||
}
|
||||
|
||||
@RequestMapping(value="/api/comments", method=RequestMethod.POST)
|
||||
public @ResponseBody List<Comment> comments(
|
||||
@RequestParam(value="id", required=true) Long id,
|
||||
@RequestParam(value="author", required=true) String author,
|
||||
@RequestParam(value="text", required=true) String text) {
|
||||
service.add(new Comment(id, author, text));
|
||||
return service.getAll();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.pavnay.demo.springboot.react.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import fr.pavnay.demo.springboot.react.domain.Comment;
|
||||
|
||||
@Component
|
||||
public class CommentService {
|
||||
|
||||
private List<Comment> comments = new ArrayList<Comment>();
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
comments.addAll(Arrays.asList(new Comment(1L, "Pete Hunt", "This is one comment"),
|
||||
new Comment(2L, "Jordan Walke", "This is *another* comment")));
|
||||
}
|
||||
|
||||
public void add(Comment comment) {
|
||||
comments.add(comment);
|
||||
}
|
||||
|
||||
public List<Comment> getAll() {
|
||||
return comments;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package fr.pavnay.demo.springboot.react.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
public class JavaScriptEngine {
|
||||
|
||||
private final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
|
||||
public JavaScriptEngine polyfillToNashorn() {
|
||||
this.loadFromClassPath("static/js/nashorn-polyfill.js");
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaScriptEngine eval(String script) {
|
||||
try {
|
||||
this.scriptEngine.eval(script);
|
||||
} catch (ScriptException e) {
|
||||
throw new IllegalStateException("Failed to eval " + script + "!", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JavaScriptEngine loadFromClassPath(String file) {
|
||||
try {
|
||||
this.scriptEngine.eval(readFromClassPath(file));
|
||||
} catch (ScriptException e) {
|
||||
throw new IllegalStateException("Failed to loadFromClassPath " + file + "!", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Object invokeFunction(String functionName, Object... args) {
|
||||
try {
|
||||
return ((Invocable) this.scriptEngine).invokeFunction(functionName, args);
|
||||
} catch (ScriptException | NoSuchMethodException e) {
|
||||
throw new IllegalArgumentException("Failed to invoke " + functionName, e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T invokeFunction(String functionName, Function<Object, T> converter, Object... args) {
|
||||
return converter.apply(invokeFunction(functionName, args));
|
||||
}
|
||||
|
||||
private String readFromClassPath(String path) {
|
||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream(path)) {
|
||||
if (in == null) {
|
||||
throw new IllegalArgumentException(path + " is not found!");
|
||||
}
|
||||
return copyToString(in, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to read " + path, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String copyToString(InputStream in, Charset charset) throws IOException {
|
||||
StringBuilder out = new StringBuilder();
|
||||
try (InputStreamReader reader = new InputStreamReader(in, charset);) {
|
||||
char[] buffer = new char[4096];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = reader.read(buffer)) != -1) {
|
||||
out.append(buffer, 0, bytesRead);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package fr.pavnay.demo.springboot.react.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import fr.pavnay.demo.springboot.react.domain.Comment;
|
||||
import fr.pavnay.demo.springboot.react.service.CommentService;
|
||||
import fr.pavnay.demo.springboot.react.util.JavaScriptEngine;
|
||||
|
||||
@Controller
|
||||
public class ViewController {
|
||||
|
||||
@Autowired
|
||||
private CommentService service;
|
||||
|
||||
@Bean
|
||||
JavaScriptEngine nashornEngine() {
|
||||
return new JavaScriptEngine().polyfillToNashorn()
|
||||
.loadFromClassPath("static/js/vendors.bundle.js")
|
||||
.loadFromClassPath("static/js/app.bundle.js");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
@Autowired
|
||||
JavaScriptEngine nashorn;
|
||||
|
||||
@RequestMapping("/greeting")
|
||||
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
|
||||
Model model) {
|
||||
model.addAttribute("name", name);
|
||||
return "greeting";
|
||||
}
|
||||
|
||||
@RequestMapping("/index")
|
||||
public String index(Model model) throws JsonProcessingException {
|
||||
List<Comment> comments = service.getAll();
|
||||
String markup = nashorn.invokeFunction("renderServer", String::valueOf, comments);
|
||||
String data = objectMapper.writeValueAsString(comments);
|
||||
model.addAttribute("markup", markup);
|
||||
model.addAttribute("data", data);
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user