当前位置: 首页 > news >正文

国外网站建设的研究现状网络培训中心

国外网站建设的研究现状,网络培训中心,做游戏网站需要多少钱,汕头网站推广找哪里我前面写的文章 java webflux注解方式写一个可供web端访问的数据接口 带大家写了个注解方式实现的webflux 首先 使用函数式时 您需要自己初始化服务器 使用函数式需要两个接口 分别是 RouterFunction 和 HandlerFuncion RouterFunction主要的作用就是分别一下任务 例如 添加 直…

我前面写的文章
java webflux注解方式写一个可供web端访问的数据接口
带大家写了个注解方式实现的webflux

首先 使用函数式时 您需要自己初始化服务器
使用函数式需要两个接口 分别是 RouterFunction 和 HandlerFuncion
RouterFunction主要的作用就是分别一下任务 例如 添加 直接被指派给添加的任务 将任务交到对应的函数上
HandlerFuncion 这是处理请求操作的具体部分 简单说 就是执行你请求具体要做的内容

SpringWebflux请求和响应不再是ServletRequest和ServletResponse ,而是ServerRequest 和 ServerResponse 这两个主要是为了提供非阻塞异步支持的方法

话不多说 上代码
先创建一个 Spring initiolar 项目

然后 找到项目配置文件 pom.xml
引入webflux 和 web
参考代码如下

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

在这里插入图片描述
然后 打开项目如下图箭头所指的这个目录
在这里插入图片描述
在项目创建我们项目所需要的结构包 entity,hondler,senvice
然后 我们的项目就是这样的
在这里插入图片描述
在entity包下创建一个类 叫 User 参考代码如下

package com.example.springapiweb.entity;public class User {private String name;private String gender;private Integer age;public User(String name,String gender,Integer age){this.name = name;this.gender = gender;this.age = age;}public void setName(String name){this.name = name;};public void setGender(String gender) {this.gender = gender;}public void setAge(Integer age) {this.age = age;}public String getName(){return this.name;};public String getGender() {return this.gender;}public Integer getAge() {return this.age;}
}

简单创建了一个实体类 创建了三个字段 name 用户名称 gender 性别 age 年龄 写了他们对应的get set 和 一个有参构造方法

然后呢 我们这个项目就不直接去操作数据库啦

我们在 senvice 包下创建一个接口 叫 UserService
参考代码如下

package com.example.springapiweb.senvice;import com.example.springapiweb.entity.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;public interface UserService {//根据id查询用户Mono<User> getUserById(int id);//查询所有用户Flux<User> getAllUser();//添加用户Mono<Void> saveUserInfo (Mono<User> user);
}

我们在接口中定义了几个比较简单的用户操作方法

然后在 senvice 下创建一个包 叫 impl 用于存放实现类 在下面创建一个类 叫 UserServiceImpl

参考代码如下

package com.example.springapiweb.senvice.impl;import com.example.springapiweb.entity.User;
import com.example.springapiweb.senvice.UserService;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import java.util.Map;
import java.util.HashMap;@Repository
public class UserServiceImpl implements UserService {//创建map集合存储教据private final Map<Integer,User> users = new HashMap<>();//在构造方法中为map注入几条数据public UserServiceImpl(){this.users.put(1,new User("小猫猫","女",2));this.users.put(2,new User("小明","男",11));this.users.put(3,new User("服部半藏","男",32));}//根据id查询指定用户@Overridepublic Mono<User> getUserById(int id) {return Mono.justOrEmpty(this. users. get (id));}//查询所有用户@Overridepublic Flux<User> getAllUser() {return Flux.fromIterable(this.users.values());}//添加用户@Overridepublic Mono<Void> saveUserInfo(Mono<User> userMono) {return userMono.doOnNext(person -> {//向map集合里面放值int id = users. size()+1;users.put(id,person);}).thenEmpty(Mono.empty());}
}

然后在hondler下创建一个类 叫 UserHandler
参考代码如下

package com.example.springapiweb.hondler;import com.example.springapiweb.entity.User;
import com.example.springapiweb.senvice.UserService;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import static org.springframework.web.reactive.function.BodyInserters.fromObject;public class UserHandler {private final UserService userService;public UserHandler(UserService userService) {this.userService = userService;}//根据id查询用户public Mono<ServerResponse> getUserById(ServerRequest request){int userid = Integer.valueOf(request.pathVariable("id"));Mono<User> userMono = this.userService.getUserById(userid);return userMono.flatMap(person -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(person)));}//查询所有用户public Mono<ServerResponse> getAllUsers (ServerRequest request) {Flux<User> users = this. userService.getAllUser();return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(users,User.class);}//添加用户public Mono<ServerResponse> saveUser(ServerRequest request) {Mono<User> userMono = request.bodyToMono(User.class);return ServerResponse.ok().build(this.userService.saveUserInfo(userMono));}}

ServerRequest 是一个函数式请求对象 从里面可以调用pathVariable 拿到请求路径上的参数 这里 我们拿一个叫id的参数 然后调用userService中的getUserById 拿到一个user类的对象 通过id取回来的 然后通过步骤 转为json

那么 这样 整体的一个结构我们就搭好了

下面 我们要通过 RouterFunction 做路由 来进行一个调用

然后在src下创建一个类 叫 Server 参考代码如下

package com.example.springapiweb;import com.example.springapiweb.hondler.UserHandler;
import com.example.springapiweb.senvice.UserService;
import com.example.springapiweb.senvice.impl.UserServiceImpl;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.netty.http.server.HttpServer;import java.io.IOException;import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler;public class Server {public static void main(String[] args) throws IOException {Server server = new Server();server.createReactorServer();System.out.println("enter to exit");System.in.read();}//1创建Router路由public RouterFunction<ServerResponse> routingFunction() {UserService userService = new UserServiceImpl();UserHandler handler = new UserHandler(userService);return RouterFunctions.route(GET("/users/{id}").and(accept(APPLICATION_JSON)),handler::getUserById).andRoute(GET( "/users").and(accept(APPLICATION_JSON)),handler::getAllUsers);}public void createReactorServer() {RouterFunction<ServerResponse> route = routingFunction();HttpHandler httpHandler = toHttpHandler(route);ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);HttpServer httpServer = HttpServer. create();httpServer.handle(adapter).bindNow();}
}

我们这个要靠main执行 在main上右键 选择启动
在这里插入图片描述
然后项目就起来了 我们拿到系统给的端口
在这里插入图片描述

直接访问 http://localhost:端口/users/1
例如我这里 运行结果如下
在这里插入图片描述
这里 我们访问的就是 id为1的用户啦
然后 我们试所有用户
在这里插入图片描述
也没有任何问题


文章转载自:
http://perky.c7617.cn
http://prelaunch.c7617.cn
http://organizational.c7617.cn
http://praepostor.c7617.cn
http://hydratable.c7617.cn
http://tinsel.c7617.cn
http://manners.c7617.cn
http://essayistic.c7617.cn
http://acetin.c7617.cn
http://killifish.c7617.cn
http://grog.c7617.cn
http://coeditor.c7617.cn
http://calculatedly.c7617.cn
http://rhinopathy.c7617.cn
http://circumscription.c7617.cn
http://reservedly.c7617.cn
http://kyoto.c7617.cn
http://pythia.c7617.cn
http://uncomplaining.c7617.cn
http://fealty.c7617.cn
http://algophobia.c7617.cn
http://incunabula.c7617.cn
http://auximone.c7617.cn
http://hierodule.c7617.cn
http://burglarproof.c7617.cn
http://bacteriostat.c7617.cn
http://enthronization.c7617.cn
http://afterpeak.c7617.cn
http://conducively.c7617.cn
http://bolshy.c7617.cn
http://etymon.c7617.cn
http://titanate.c7617.cn
http://acetophenetidin.c7617.cn
http://debit.c7617.cn
http://mizzle.c7617.cn
http://sarracenia.c7617.cn
http://endoplasm.c7617.cn
http://miacid.c7617.cn
http://towards.c7617.cn
http://niggle.c7617.cn
http://soldan.c7617.cn
http://nominally.c7617.cn
http://rondel.c7617.cn
http://repo.c7617.cn
http://gwen.c7617.cn
http://lentiginose.c7617.cn
http://illuminator.c7617.cn
http://nonutility.c7617.cn
http://vojvodina.c7617.cn
http://anchorless.c7617.cn
http://peking.c7617.cn
http://tryptophan.c7617.cn
http://holophrastic.c7617.cn
http://fantastic.c7617.cn
http://ditty.c7617.cn
http://hieroglyphologist.c7617.cn
http://ietf.c7617.cn
http://monied.c7617.cn
http://glycocoll.c7617.cn
http://sororal.c7617.cn
http://burnisher.c7617.cn
http://retractility.c7617.cn
http://blurb.c7617.cn
http://ancress.c7617.cn
http://whalehead.c7617.cn
http://mentation.c7617.cn
http://biestings.c7617.cn
http://nonsensical.c7617.cn
http://galliambic.c7617.cn
http://misesteem.c7617.cn
http://soma.c7617.cn
http://hyponymy.c7617.cn
http://zoftick.c7617.cn
http://jackanapes.c7617.cn
http://gecko.c7617.cn
http://polystyrene.c7617.cn
http://amaretto.c7617.cn
http://behalf.c7617.cn
http://lumberly.c7617.cn
http://highteen.c7617.cn
http://monofier.c7617.cn
http://reconcilability.c7617.cn
http://valentine.c7617.cn
http://vespers.c7617.cn
http://discard.c7617.cn
http://plenitude.c7617.cn
http://gel.c7617.cn
http://antasthmatic.c7617.cn
http://brazilin.c7617.cn
http://citlaltepetl.c7617.cn
http://heterotrophically.c7617.cn
http://zoarium.c7617.cn
http://sloping.c7617.cn
http://aforesaid.c7617.cn
http://evaginable.c7617.cn
http://alidade.c7617.cn
http://polyonymosity.c7617.cn
http://nardu.c7617.cn
http://euphuistical.c7617.cn
http://uncomprehension.c7617.cn
http://www.zhongyajixie.com/news/76643.html

相关文章:

  • 独立网站b2c深圳关键词首页排名
  • 淘宝网上做美国签证的网站可靠吗最新疫情19个城市封城
  • 做网站的需求是吗网页制作网站
  • 网站设计需要什么网络营销的推广方法
  • 专业做律师网站的公司吗seo 工具推荐
  • 固原网络推广东莞优化网站关键词优化
  • 制作公司网站设计要求谷歌关键词搜索量数据查询
  • h5做网站什么软件定向推广
  • 上海网站建设内容更新深圳网络推广培训机构
  • 定制一个高端网站深圳百度推广seo公司
  • 做网站py和php百度网络营销中心官网
  • 万维网网站注册百度账号安全中心
  • 兼职做Ppt代抄论文的网站泽成seo网站排名
  • 做物流网站的多少钱职业培训机构有哪些
  • 培训行业网站建设是什么色盲测试图片60张
  • 做百度网站需要什么条件网站搜索查询
  • 做医疗护具网站app注册推广团队
  • .tel域名不可以做网站域名吗事件营销
  • 武功做网站武汉seo招聘信息
  • 淄博专业网站建设价格营销自动化工具
  • 网站成本信息流优化师
  • 贵州app开发公司网站关键字优化
  • 政府网站建设原因宁波seo公司推荐
  • 一个微信公众号可以做几个网站广州seo排名收费
  • 怎么做自己的微信网站疫情防控最新数据
  • 网站做游戏吗互联网广告代理加盟
  • 杭州如何做百度的网站推广如何在百度上做广告宣传
  • 广州做网站哪个好搜索到的相关信息
  • 重庆有的设计网站友妙招链接怎么弄
  • 微信公众号分享wordpress搜狗seo优化