Skip to content

解决RequestBody注解无法支持多个参数问题,传统形式必须封装对象,导致对象泛滥。同时解决request流反复读问题

Notifications You must be signed in to change notification settings

dzw1113/multirequest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started

maven引用

<dependency>
    <groupId>io.github.dzw1113</groupId>
    <artifactId>multirequest</artifactId>
    <version>1.0.1</version>
</dependency>

@RequestBody:并不能支持多个参数,当有多个参数时,必须封装成对象,较麻烦

@MultiRequestBody:支持关系映射、多参数(基础类型、集合、对象),支持Spring Validated校验。支持Swagger2的话,需要额外配置代码。

用法: 在controller方法种直接引用:

public HttpResult<HdTodayReviewPO> get(@MultiRequestBody HdTodayReviewQueryPO hdTodayReviewQueryPO) 

public HttpResult check(@MultiRequestBody(required = true) Long id, @MultiRequestBody(required = true) String operationCode) {

过滤器:BodyFilter,支持反复读取Request里的流,需自己注入到FilterRegistrationBean里。

注入:

@Bean
@Order(3)
public FilterRegistrationBean<BodyFilter> Filters() {
    FilterRegistrationBean<BodyFilter> registrationBean = new FilterRegistrationBean<BodyFilter>();
    registrationBean.setFilter(new BodyFilter());
    registrationBean.addUrlPatterns("/*");
    registrationBean.setAsyncSupported(Boolean.TRUE);
    registrationBean.setName("bodyFilter");
    return registrationBean;
}

取法:

HttpServletRequest request = servletRequestAttributes.getRequest();
jsonBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
RequestUtils.getRequest().setAttribute("JSON_REQUEST_BODY", jsonBody);

About

解决RequestBody注解无法支持多个参数问题,传统形式必须封装对象,导致对象泛滥。同时解决request流反复读问题

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages