springboot web flux

1.

2.

public class Person {
	private int id;
	
	private String name;
 
 
	public int getId() {
		return id;
	}
 
	public void setId(int id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + "]";
	}
	
	
}

3.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.example.springbootjdbc.bean.Person;
import com.example.springbootjdbc.repository.PersonRepository;
 
@RestController
public class PersonController {
	@Autowired
	private PersonRepository personRepository;
	
	@RequestMapping("mvc/person/save")
	public boolean save(@RequestBody Person person) {
		return personRepository.save(person);
	}
 
}

4.

import java.util.Collection;
import java.util.Collections;
 
import org.springframework.stereotype.Repository;
 
import com.example.springbootjdbc.bean.Person;
 
@Repository
public class PersonRepository {
	
	public Boolean save(Person person) {
		System.out.println("save Person:"+person);
		return true;
	}
	public Collection<Person> findAll(){
		return Collections.emptyList();
	}
 
}

5.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
 
import com.example.springbootjdbc.bean.Person;
import com.example.springbootjdbc.repository.PersonRepository;
 
import reactor.core.publisher.Mono;
 
@Component
public class PersonHandler {
	
	@Autowired
	private PersonRepository personRepository;
	
	public Mono<ServerResponse> save(ServerRequest serverRequest){
		//在springmvc中用@requestbody
		//在web flux中使用ServerRequest
		//Mono<Person> 类似于Optional<User>
		Mono<Person> personMono=serverRequest.bodyToMono(Person.class);
		//map相当于转化工作
		Mono<Boolean> booleanMono=personMono.map(personRepository::save);
		return ServerResponse.ok().body(booleanMono,Boolean.class);
		
	}
		
}

6.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
 
@Configuration
public class WebFluxConfiguretion {
 
	@Bean
	public RouterFunction<ServerResponse> savePerson(PersonHandler personHandler){
		return RouterFunctions.route(RequestPredicates.POST("/flux/person/save"), personHandler::save);
	}
}

Mono: 0-1个元素 

Flux: 0-N个元素

引入Reporitory的Jar包,可直接操作数据库。。。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
    </dependency>

 在目前的Spring WebFlux还没有支持类似Mysql这样的关系型数据库,现在一般做mongodb的存储

猜你喜欢

转载自blog.csdn.net/q975583865/article/details/83349643