Думаю, я сделаю это безумно: (
, потому что ServerRequest
, потратил день.
- WebClient (Chrome Browser) ajax тип записи.
- WebServer (SpringBoot Webflux) использует обработчик и маршрутизатор.
- WebSecurity использует spring-boot-security.
Моя проблема заключается в следующем.
Chrome браузер DevelopTool
браузер использовать DevelopTool, сетевой кран является проверка заголовка. FormData - это успех ввода данных.
Но !!!
java проверка журнала
я не знаю! в чем причина. Так что я схожу с ума.
Я представляю источник.
JAVASCRIPT AJAX
$(document).ready(function () {
var data = {"name": "jskang"};
var strData = JSON.stringify(data);
var formData = new FormData();
formData.append("name", "jskang123");
$.ajax({
url: "/statistics/systemEnvironment/function2",
type: "post",
data: strData,
contentType: "application/json",
success: function (data) {
console.log(data);
}
});
});
Сервер Webflux Router
@Configuration
@EnableWebFlux
public class RouterConfig implements ApplicationContextAware, WebFluxConfigurer {
private ApplicationContext context;
@Bean /* menu setting */
public RouterFunction<ServerResponse> preSystemEnvironmentRoutes(SystemEnvironmentHandler handler) {
return route()
.GET("/statistics/systemEnvironment/function1/{name}", accept(MediaType.APPLICATION_JSON), handler::function1)
.POST("/statistics/systemEnvironment/function2", accept(MediaType.APPLICATION_JSON), handler::function2)
.build();
}
//////* Thymeleaf webflux setting start *//////
@Override
public void setApplicationContext(ApplicationContext context) {
this.context = context;
}
@Bean
public ITemplateResolver thymeleafTemplateResolver() {
final SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(this.context);
resolver.setPrefix("classpath:templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(false);
resolver.setCheckExistence(false);
return resolver;
}
@Bean
public ISpringWebFluxTemplateEngine thymeleafTemplateEngine() {
SpringWebFluxTemplateEngine templateEngine = new SpringWebFluxTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public ThymeleafReactiveViewResolver thymeleafReactiveViewResolver() {
ThymeleafReactiveViewResolver viewResolver = new ThymeleafReactiveViewResolver();
viewResolver.setTemplateEngine(thymeleafTemplateEngine());
return viewResolver;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafReactiveViewResolver viewResolver = new ThymeleafReactiveViewResolver();
viewResolver.setTemplateEngine(thymeleafTemplateEngine());
registry.viewResolver(thymeleafReactiveViewResolver());
}
//////* Thymeleaf webflux setting end *//////
}
}
Сервер Webflux Handler
@Component
public class SystemEnvironmentHandler {
/**
* The constant LOG.
*/
private static final Logger LOG = getLogger(SystemEnvironmentHandler.class);
public Mono<ServerResponse> function1(ServerRequest request) {
LOG.info("SystemEnvironmentHandler function1 run");
String variable = request.pathVariable("name");
Mono<String> response = Mono.just(variable);
return ServerResponse.ok().body(response, String.class);
}
public Mono<ServerResponse> function2(ServerRequest request) {
LOG.info("SystemEnvironmentHandler function1 run");
System.out.println("\n========= request check ==========");
System.out.println(request.attributes().get("name"));
System.out.println(request.formData().toProcessor());
System.out.println(request.queryParams());
System.out.println(request);
System.out.println("\n========= mono1 check ==========");
Mono<Name> mono1 = request.bodyToMono(new Name().getClass());
System.out.println(mono1);
System.out.println("\n========= mono2 check ==========");
Mono<Name> mono2 = request.bodyToMono(Name.class);
System.out.println(mono2);
System.out.println("\n========= mono3 check ==========");
Mono<MultiValueMap<String, String>> mono3 = request.body(BodyExtractors.toFormData());
System.out.println(mono3);
System.out.println(mono3.toProcessor().peek());
System.out.println("\n========= formdata check ==========");
MultiValueMap formData = request.exchange().getFormData().toProcessor().peek();
System.out.println(formData);
Mono<String> response = Mono.just(new String("SystemEnvironmentHandler - function2"));
return ServerResponse.ok().body(response, String.class);
}
public class Name{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}