欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单使用(搭建)SpringFu

程序员文章站 2022-06-28 08:02:37
官方网址:https://spring.io/blog/2018/10/02/the-evolution-of-spring-fugithub:https://github.com/spring-projects-experimental/spring-fu官方示例github:https://github.com/spring-projects-experimental/spring-fu/tree/master/samples会持续更新......所需环境:SpringBoot2.....

 

官方网址:https://spring.io/blog/2018/10/02/the-evolution-of-spring-fu

github:https://github.com/spring-projects-experimental/spring-fu

官方示例github:https://github.com/spring-projects-experimental/spring-fu/tree/master/samples

会持续更新......

所需环境:

SpringBoot2.4.0以上;

尽量会labdma表达式。

所需pom

<!-- 添加web支持,让项目持续运行 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- jafu -->
<dependency>
	<groupId>org.springframework.fu</groupId>
	<artifactId>spring-fu-jafu</artifactId>
	<version>0.4.3</version>
</dependency>

<!-- jafu运行时支持 -->
<dependency>
    <groupId>org.springframework.fu</groupId>
	<artifactId>spring-fu-autoconfigure-adapter</artifactId>
	<version>0.4.3</version>
	<scope>runtime</scope>
</dependency>

Application类编写

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.fu.jafu.BeanDefinitionDsl;
import org.springframework.fu.jafu.JafuApplication;
import org.springframework.fu.jafu.webmvc.WebMvcServerDsl;
import org.springframework.web.servlet.function.RouterFunctions;
import static org.springframework.fu.jafu.Jafu.webApplication;
import static org.springframework.fu.jafu.webmvc.WebMvcServerDsl.webMvc;


public class SpringFuApplication {

	// 配置
	public static JafuApplication app = webApplication(applicationDsl -> applicationDsl.beans(beanDefinitionDsl -> initBeans(beanDefinitionDsl))
			.enable(webMvc(serverDsl -> enableMvc(serverDsl))));

	// 启用mvc
	public static ApplicationContextInitializer<GenericApplicationContext> enableMvc(WebMvcServerDsl serverDsl) {
		                 // 设置端口号
		return serverDsl.port(serverDsl.profiles().contains("test") ? 8181 : 8080)
				.router(router -> initRouter(serverDsl, router)).converters(c -> initConverters(serverDsl));
	}
	
	// 设置格式转换
	public static WebMvcServerDsl initConverters(WebMvcServerDsl serverDsl) {
		return serverDsl.converters(c -> c.string().jackson());
	}

	// 设置接口地址路由
	public static WebMvcServerDsl initRouter(WebMvcServerDsl serverDsl, RouterFunctions.Builder router) {
		TestHandler handler = serverDsl.ref(TestHandler.class);
		router.GET("/", handler::hello);
		router.GET("/json", handler::json);
		router.GET("/json2", handler::json2);
		return serverDsl;
	}

	// 声明bean
	public static BeanDefinitionDsl initBeans(BeanDefinitionDsl beanDefinitionDsl) {
		beanDefinitionDsl
				.bean(TestHandler.class)
				.bean(TestService.class)
				.bean(TestService2.class);
		return beanDefinitionDsl;
	}

	public static void main (String[] args) {
		app.run(args);
	}

}

Handler:相当于mvc的Controller

import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;
import static org.springframework.web.servlet.function.ServerResponse.ok;
import java.util.Map;

public class TestHandler {

    /**
     * service
     */
    private TestService testService;


    /**
     * service
     */
    private TestService2 testService2;

    /**
     * 通过构造函数注入Service
     * @param testService
     */
    public TestHandler(TestService testService) {
        this.testService = testService;
    }

    /**
     * 通过构造函数注入Service
     * @param testService
     */
    public TestHandler(TestService testService, TestService2 testService2) {
        this.testService = testService;
        this.testService2 = testService2;
    }

    /**
     * 接口
     * @param request
     * @return
     */
    public ServerResponse hello(ServerRequest request) {
        System.out.println("hello");
        return ok().body("hello;");
    }

    /**
     * 接口
     * @param request
     * @return
     */
    public ServerResponse json(ServerRequest request) {
        Map<String, String> map = this.testService.json();
        return ok().body(map);
    }

    /**
     * 接口
     * @param request
     * @return
     */
    public ServerResponse json2(ServerRequest request) {
        Map<String, String> map = this.testService2.json();
        return ok().body(map);
    }
}

 Service:没有编写接口,只编写了实现类

public class TestService {

    public Map<String, String> json() {
        Map<String, String> map = new HashMap<>(1);
        map.put("zhangsan", "666");
        return map;
    }
}
public class TestService2 {

    public Map<String, String> json() {
        Map<String, String> map = new HashMap<>(1);
        map.put("lisi", "777");
        return map;
    }
}

 

本文地址:https://blog.csdn.net/weixin_42600999/article/details/111993779

相关标签: SpringFu spring