Простая минимальная реализация
This commit is contained in:
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
## IDEA ##
|
||||||
|
.idea/
|
||||||
|
out/
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
*.iws
|
||||||
|
*.ids
|
||||||
|
|
||||||
|
## ECLIPSE ##
|
||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
|
||||||
|
## MAVEN ##
|
||||||
|
target/
|
||||||
100
pom.xml
Normal file
100
pom.xml
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>Skeleton - Spring MVC</name>
|
||||||
|
|
||||||
|
<groupId>ru.dmitriymx.skeleton</groupId>
|
||||||
|
<artifactId>spring-mvc</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<slf4j.version>1.7.21</slf4j.version>
|
||||||
|
<spring.version>4.2.5.RELEASE</spring.version>
|
||||||
|
<jetty.version>9.4.0.v20161208</jetty.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- LOGGER -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SPRING -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-core</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- JETTY -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-server</artifactId>
|
||||||
|
<version>${jetty.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-webapp</artifactId>
|
||||||
|
<version>${jetty.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- COMPONENTS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.16.16</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.15</version>
|
||||||
|
<configuration>
|
||||||
|
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
||||||
99
src/main/java/ru/dmitriymx/skeleton/springmvc/WebApp.java
Normal file
99
src/main/java/ru/dmitriymx/skeleton/springmvc/WebApp.java
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package ru.dmitriymx.skeleton.springmvc;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.servlet.ServletHolder;
|
||||||
|
import org.eclipse.jetty.util.log.Log;
|
||||||
|
import org.eclipse.jetty.util.log.Slf4jLog;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class WebApp {
|
||||||
|
private final String host;
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Данный класс является файлом настройки контекста для Spring
|
||||||
|
* однако, т.к. мы никакие бины не объявляем, то красс пустует
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
public static class SpringConfigMVC extends WebMvcConfigurerAdapter {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Создаем Spring-контекст
|
||||||
|
*
|
||||||
|
* @return {@link WebApplicationContext}
|
||||||
|
*/
|
||||||
|
private WebApplicationContext getWebApplicationContext() {
|
||||||
|
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||||
|
context.register(SpringConfigMVC.class);
|
||||||
|
context.register(WebAppController.class);
|
||||||
|
// или
|
||||||
|
// context.setConfigLocation("ru.dmitriymx.skeleton.springmvc");
|
||||||
|
// в этом случае будет просканирован весь пакет на @Configuration
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Подготавливаем обработчик запросов
|
||||||
|
*
|
||||||
|
* @param context Spring-контекст веб-приложения {@link WebApplicationContext}
|
||||||
|
* @return {@link ServletContextHandler}
|
||||||
|
*/
|
||||||
|
private ServletContextHandler getServletContextHandler(WebApplicationContext context) {
|
||||||
|
ServletContextHandler contextHandler = new ServletContextHandler();
|
||||||
|
contextHandler.setErrorHandler(null);
|
||||||
|
contextHandler.setContextPath("/");
|
||||||
|
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
|
||||||
|
contextHandler.addEventListener(new ContextLoaderListener(context));
|
||||||
|
return contextHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Запуск встроенного Jetty веб-сервера
|
||||||
|
*/
|
||||||
|
private void start() {
|
||||||
|
Log.setLog(new Slf4jLog("Jetty.Logger"));
|
||||||
|
Server server = new Server(new InetSocketAddress(host, port));
|
||||||
|
server.setHandler(getServletContextHandler(getWebApplicationContext()));
|
||||||
|
try {
|
||||||
|
server.start();
|
||||||
|
server.join();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error start server", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Для запуска можно указать два параметра: хост и порт.
|
||||||
|
* По-умолчанию: 127.0.0.1:8080
|
||||||
|
*
|
||||||
|
* @param args параметры запуска
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String host = "127.0.0.1";
|
||||||
|
int port = 8080;
|
||||||
|
|
||||||
|
if (args.length == 2) {
|
||||||
|
host = args[0];
|
||||||
|
port = Integer.parseInt(args[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Web app listen: {}:{}", host, port);
|
||||||
|
WebApp app = new WebApp(host, port);
|
||||||
|
app.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.dmitriymx.skeleton.springmvc;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Здесь описываются все пути, что начинаются с '/'
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/")
|
||||||
|
public class WebAppController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Обработка корневого запроса '/'
|
||||||
|
*
|
||||||
|
* @return Сырое строковое значение
|
||||||
|
*/
|
||||||
|
@RequestMapping
|
||||||
|
@ResponseBody
|
||||||
|
public String index() {
|
||||||
|
return "Hello world!";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user