关于在Eclipse中spring项目类的动态引入

在eclipse中创建spring项目,使用spring Webflux中的Router Function技术进行动态引入

如下面的代码进行动态引入

@Bean
	RouterFunction<ServerResponse> userRouter(UserHandler handler){
		return RouterFunctions.nest(RequestPredicates.path("/user"),
				RouterFunctions.route(RequestPredicates.GET("/"),
						handler::getAllUser));
	}

对上面的代码RouterFunctionsRequestPredicates类进行动态引入

  1. 在eclipse上任务栏选择Widow--->Preferences

sa

  2.进入后选择 Java -->Editor-->Content Assist-->Favorties 过后选择New Type

aaa

3.进入 选择Browes

4.弹出框 输入你要加入的类名

aa

5.点击加入 并点击ok退出

6.在项目中删除相关的类名,这时会报错,如图

6.点击外面的红叉 并选择

依次在报错的地方引入

最后前后两次的代码对比:

package com.xf.routers;

import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.xf.handler.UserHandler;

@Configuration
public class AllRouters {
	
	/*@Bean
	RouterFunction<ServerResponse> userRouter(UserHandler handler){
		return RouterFunctions.nest(RequestPredicates.path("/user"),
				RouterFunctions.route(RequestPredicates.GET("/"),
						handler::getAllUser));
	}*/

	
	//使用动态引入
	@Bean
	RouterFunction<ServerResponse> userRouter(UserHandler handler){
		return nest
				//相当于类上面的@RequestMapping("/user")
				(path("/user"),
				//下面的相当于类里面的@RequestMapping
				//得到所有用户
				route(GET("/"),handler::getAllUser)
				//创建用户
				.andRoute(POST("/").and(accept(MediaType.APPLICATION_JSON_UTF8)),
						handler::createUser)
				//删除用户
				.andRoute(DELETE("/{id}"), handler::deleteUserById));		
	}
}

注:这里使用的Spring WebFlux 不是使用Sring MVC

      Spring WebFlux详细解析见:https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-                 framework-reference/html/web-reactive.html

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RemainderTime

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值