Created
May 4, 2019 16:57
-
-
Save jrgleason/d86c6ac489f7c77aa9eadcc273ed445c to your computer and use it in GitHub Desktop.
Spring Boot Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import java.io.File; | |
import java.util.logging.Logger; | |
import org.springframework.boot.*; | |
import org.springframework.boot.autoconfigure.*; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
@SpringBootApplication | |
@EnableAutoConfiguration | |
public class Application extends WebMvcConfigurerAdapter{ | |
public static void main(String[] args){ | |
SpringApplication.run(Application.class, args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import java.util.logging.Logger; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import javax.servlet.http.HttpServletResponse; | |
import org.springframework.ui.Model; | |
import org.example.AttackService; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
@RestController | |
public class DataController{ | |
private static Logger log = Logger.getLogger("Data Controller"); | |
@Autowired | |
AttackService attack; | |
@GetMapping("ping") | |
String get(){ | |
return "Worked"; | |
} | |
@RequestMapping("/") | |
String getIndex(Model model){ | |
model.addAttribute("key", "My Key"); | |
return "home"; | |
} | |
@GetMapping(path = "/attacks.json", produces = "application/json") | |
String getData(HttpServletResponse response){ | |
try{ | |
return attack.getReport(); | |
} | |
catch(Exception ex){ | |
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | |
return ex.toString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Description; | |
import org.springframework.web.servlet.ViewResolver; | |
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
import org.thymeleaf.spring5.SpringTemplateEngine; | |
import org.thymeleaf.spring5.view.ThymeleafViewResolver; | |
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
import java.util.logging.Logger; | |
import java.io.File; | |
@Configuration | |
public class WebConfig implements WebMvcConfigurer { | |
private static Logger log = Logger.getLogger("Application"); | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
String currentPath = new File(".").getAbsolutePath(); | |
log.info("file:///"+currentPath+"/../ui/dist"); | |
registry.addResourceHandler("/ui/**") | |
.addResourceLocations("file:///"+currentPath+"/../ui/dist/"); | |
} | |
@Bean | |
@Description("Thymeleaf template resolver serving HTML 5") | |
public ClassLoaderTemplateResolver templateResolver() { | |
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); | |
templateResolver.setPrefix("templates/"); | |
templateResolver.setCacheable(false); | |
templateResolver.setSuffix(".html"); | |
templateResolver.setTemplateMode("HTML5"); | |
templateResolver.setCharacterEncoding("UTF-8"); | |
return templateResolver; | |
} | |
@Bean | |
@Description("Thymeleaf template engine with Spring integration") | |
public SpringTemplateEngine templateEngine() { | |
SpringTemplateEngine templateEngine = new SpringTemplateEngine(); | |
templateEngine.setTemplateResolver(templateResolver()); | |
return templateEngine; | |
} | |
@Bean | |
@Description("Thymeleaf view resolver") | |
public ViewResolver viewResolver() { | |
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); | |
viewResolver.setTemplateEngine(templateEngine()); | |
viewResolver.setCharacterEncoding("UTF-8"); | |
return viewResolver; | |
} | |
@Override | |
public void addViewControllers(ViewControllerRegistry registry) { | |
registry.addViewController("/other").setViewName("home"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment