<dependency>
<groupId>cn.taketoday</groupId>
<artifactId>today-framework</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
只需要
@RestController
public class DemoApplication {
@GET("/index/{q}")
public String index(String q) {
return q;
}
public static void main(String[] args) {
WebApplication.run(DemoApplication.class, args);
}
}
@Slf4j
@RestController // rest 控制器
@RestControllerAdvice
@Import(NettyApplication.AppConfig.class) // 导入配置
public class NettyApplication {
public static void main(String[] args) {
WebApplication.runReactive(NettyApplication.class, args);
}
@GET("/index")
public String index() {
return "Hello";
}
@GET("/body/{name}/{age}")
public Body index(String name, int age) {
return new Body(name, age);
}
@GET("/publish-event")
public void index(String name, @Autowired ApplicationEventPublisher publisher) {
publisher.publishEvent(new MyEvent(name));
}
@GET("/request-context")
public String context(RequestContext context) {
final String requestURL = context.requestURL();
final String queryString = context.queryString();
System.out.println(requestURL);
System.out.println(queryString);
return queryString;
}
@Getter
static class Body {
final String name;
final int age;
Body(String name, int age) {
this.name = name;
this.age = age;
}
}
@Configuration
@EnableNettyHandling
@EnableMethodEventDriven
static class AppConfig {
@EventListener(MyEvent.class)
public void event(MyEvent event) {
log.info("event :{}", event);
}
}
@ToString
static class MyEvent {
final String name;
MyEvent(String name) {
this.name = name;
}
}
@ExceptionHandler(Throwable.class)
public void throwable(Throwable throwable) {
throwable.printStackTrace();
}
}
@Slf4j
@Configuration
@RequestMapping
@EnableDefaultMybatis
@EnableRedissonCaching
@EnableTomcatHandling
@ComponentScan("cn.taketoday.blog")
@PropertiesSource("classpath:info.properties")
@MultipartConfig(maxFileSize = 10240000, fileSizeThreshold = 1000000000, maxRequestSize = 1024000000)
public class TestApplication implements WebMvcConfiguration, ApplicationListener<ContextStartedEvent> {
public static void main(String[] args) {
WebApplication.run(TestApplication.class, args);
}
@GET("index/{q}")
public String index(String q) {
return q;
}
@Singleton
@Profile("prod")
public ResourceHandlerRegistry prodResourceMappingRegistry() {
final ResourceHandlerRegistry registry = new ResourceHandlerRegistry();
registry.addResourceMapping(LoginInterceptor.class)//
.setPathPatterns("/assets/admin/**")//
.setOrder(Ordered.HIGHEST_PRECEDENCE)//
.addLocations("/assets/admin/");
return registry;
}
@Singleton
@Profile("dev")
public ResourceHandlerRegistry devRsourceMappingRegistry(@Env("site.uploadPath") String upload,
@Env("site.assetsPath") String assetsPath) //
{
final ResourceHandlerRegistry registry = new ResourceHandlerRegistry();
registry.addResourceMapping("/assets/**")//
.addLocations(assetsPath);
registry.addResourceMapping("/upload/**")//
.addLocations(upload);
registry.addResourceMapping("/logo.png")//
.addLocations("file:///D:/dev/www.yhj.com/webapps/assets/images/logo.png");
registry.addResourceMapping("/favicon.ico")//
.addLocations("classpath:/favicon.ico");
return registry;
}
@Override
public void onApplicationEvent(ContextStartedEvent event) {
log.info("----------------Application Started------------------");
}
}
本项目的诞生离不开以下开源项目:
- Slf4j: Simple Logging Facade for Java
- EL: Java Unified Expression Language
- Lombok: Very spicy additions to the Java programming language
- FastJSON: A fast JSON parser/generator for Java
- Freemarker: Apache Freemarker
- Apache Commons FileUpload: Apache Commons FileUpload
- Netty: Netty project - an event-driven asynchronous network application framework
- Jetty: Eclipse Jetty® - Web Container
- Tomcat: Apache Tomcat
- Undertow: High performance non-blocking webserver
- Today Web: A Java library for building web applications
- Today Context: A Java library for dependency injection and aspect oriented programing
- JLHTTP: Java Lightweight HTTP Server (Web Server)