SpringBoot整合MongoDb(二)

构建项目及配置

pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yml

spring:
  application:
    name: spring-boot-mongodb
  data:
    mongodb:
      uri: mongodb://test:123456@192.168.180.113:27017/test

创建实体及具体实现

package com.topcheer.mongodb2.entity;

import org.springframework.data.annotation.Id;


public class UserInfo {

    @Id
    private Long id;

    private String username;

    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserInfo(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public UserInfo() {
    }
}
@Component
public class UserHandler {

    private final UserRepository repository;

    public UserHandler(UserRepository repository) {
        this.repository = repository;
    }

    public Mono<ServerResponse> saveUser(ServerRequest request) {
        Mono<UserInfo> user = request.bodyToMono(UserInfo.class);
        return ServerResponse.ok().build(repository.insert(user).then());
    }

    public Mono<ServerResponse> deleteUser(ServerRequest request) {
        Long userId = Long.valueOf(request.pathVariable("id"));
        return ServerResponse.ok().build(repository.deleteById(userId).then());
    }

    public Mono<ServerResponse> getUser(ServerRequest request) {
        Long userId = Long.valueOf(request.pathVariable("id"));
        Mono<UserInfo> userInfo = repository.findById(userId);
        return ServerResponse.ok().contentType(APPLICATION_JSON).body(userInfo, UserInfo.class);
    }

    public Mono<ServerResponse> listUser(ServerRequest request) {
        Flux<UserInfo> userList = repository.findAll();
        return ServerResponse.ok().contentType(APPLICATION_JSON).body(userList, UserInfo.class);
    }


}
public interface UserRepository extends ReactiveMongoRepository<UserInfo,Long> {
}
package com.topcheer.mongodb2.router;

import com.topcheer.mongodb2.handler.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
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 UserRouter {
    @Bean
    public RouterFunction<ServerResponse> routeCity(UserHandler userHandler) {
        return RouterFunctions
                .route(RequestPredicates.GET("/listUser")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::listUser)
                .andRoute(RequestPredicates.GET("/user/{id}")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::getUser)
                .andRoute(RequestPredicates.GET("/deleteUser/{id}")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::deleteUser)
                .andRoute(RequestPredicates.POST("/saveUser")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::saveUser);
    }
}

测试:

 

 

posted @ 2019-11-25 14:33  天宇轩-王  阅读(270)  评论(0编辑  收藏  举报