栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot: WebFlux入门程序及跨域配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot: WebFlux入门程序及跨域配置

一、创建spring boot项目,并引入相关依赖,完整pom如下:



    4.0.0

    cn.edu.tju
    springbootwebflux
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.6
         
    

    

        
            org.springframework.boot
            spring-boot-starter-webflux
        


        
            org.springframework.boot
            spring-boot-starter-data-redis
        





        
            com.google.code.gson
            gson
            2.8.6
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            5.1.30
        




        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.13
        


        
        
            io.springfox
            springfox-swagger2
            3.0.0
        
        
        
            io.springfox
            springfox-swagger-ui
            3.0.0
        



    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

        
    



二、创建启动类:

package cn.edu.tju;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class,args);
    }
}

三、创建handler类,来提供具体的业务功能

package cn.edu.tju.handler;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Service
public class UserHandler {

    public Mono getUser(ServerRequest request){
        String str="hello";
        String id=request.pathVariable("id");
        Mono mono=Mono.just(str+","+id);

        return ServerResponse.ok().body(mono,str.getClass());
    }
}

四、创建配置类,来实现将前端发送的请求路由到业务功能类:

package cn.edu.tju.config;

import cn.edu.tju.handler.UserHandler;

//静态导入
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
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.PUT;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RequestPredicates.contentType;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;


import reactor.core.publisher.Mono;

@Configuration
public class RouteConfig {
    @Autowired
    private UserHandler userHandler;

    @Bean
    public RouterFunction userRouter(){
        RouterFunction router=route(GET("/router/user/{id}").
                and(accept(APPLICATION_OCTET_STREAM)),userHandler::getUser);

        return router;
    }
}

五、配置跨域bean

package cn.edu.tju.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class MyConfig {
    @Bean
    CorsWebFilter corsFilter() {

        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号