GVKun编程网logo

如何将所有用@RequestParam注释的字段收集到一个对象中

18

如果您对如何将所有用@RequestParam注释的字段收集到一个对象中感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何将所有用@RequestParam注释的字段收集到

如果您对如何将所有用@RequestParam注释的字段收集到一个对象中感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何将所有用@RequestParam注释的字段收集到一个对象中的详细内容,并且为您提供关于@Controller和@RequestMapping和@RequestParam注解理解和区别、@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam、@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明、@RequestBody 和 @RequestParam(“test”) 的区别与联系的有价值信息。

本文目录一览:

如何将所有用@RequestParam注释的字段收集到一个对象中

如何将所有用@RequestParam注释的字段收集到一个对象中

我想将我所有的查询参数收集到一个pojo中,并对字段进行附加验证。

我已经读到我可以简单地创建一个对象,并且spring-boot会在其上自动设置那些请求参数。

@GetMaping public ResponseEntity<?> listEntities(@RequestParam(value = "page-number", defaultValue = "0") @Min(0) Integer pageNumber, @RequestParam(value ="page-size", defaultValue = "100") @Min(1) Integer pageSize ... )

我正在考虑创建一个名为的类RequestParamsDTO,在其中让我的查询参数负责分页。

但是要在上设置这些字段RequestParamsDTO,我必须将请求参数的名称与字段名称进行匹配。但这不是有效的变量名称:page-size

必须有一些变通方法,类似于@RequestParam的value属性,它将在DTO的字段中设置给定的请求参数。

请指教。

答案1

小编典典

之前已经有人打算使用此功能,因此您可以执行以下操作。但是很遗憾,由于不活动响应,该功能被拒绝了:

public class RequestParamsDTO{   @RequestParam(value="page-number",defaultValue="0")   @Min(0)   private Integer pageNumber;   @RequestParam(value = "page-size", defaultValue = "100")    @Min(1)    Integer pageSize }

您可以做的最相似的事情是使用它@ModelAttribute来按以下顺序解析参数:

  • 从模型(如果已通过使用模型添加)。
  • 通过使用@SessionAttributes在HTTP会话中进行。
  • 来自通过Converter传递的URI路径变量(请参见下一个示例)。
  • 从默认构造函数的调用开始。
  • 从调用具有与Servlet请求参数匹配的参数的“主要构造函数”开始。参数名称是通过JavaBeans
    @ConstructorProperties或字节码中运行时保留的参数名称确定的。

这意味着RequestParamsDTO不能有任何默认构造函数(没有参数的构造函数)。它应该有一个“主要构造函数”,您可以使用它@ConstructorProperties来定义哪些请求参数映射到构造函数参数:

public class RequestParamsDTO{    @Min(0)    Integer pageNumber;    @Min(1)    Integer pageSize;    @ConstructorProperties({"page-number","page-size"})    public RequestParamsDTO(Integer pageNumber, Integer pageSize) {        this.pageNumber = pageNumber != null ? pageNumber : 0;        this.pageSize = pageSize != null ? pageSize : 100;    }}

控制器方法变为:

@GetMapingpublic ResponseEntity<?> listEntities(@Valid RequestParamsDTO request){}

笔记:

  • 没有为没有等效注释@RequestParamdefaultValue,所以需要在构造函数中手动实现。

  • 如果controller方法参数与this中的值不匹配,@ModelAttribute即使@ModelAttribute没有显式注释它,它也将被解析。

@Controller和@RequestMapping和@RequestParam注解理解和区别

@Controller和@RequestMapping和@RequestParam注解理解和区别

    在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,
然后再把该Model 返回给对应的View 进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,
只需使用@Controller 标记一个类是Controller ,
然后使用@RequestMapping 和@RequestParam 等一些注解用以定义URL 请求和Controller 方法之间的映射,这样的Controller 就能被外界访问到

 @Controller 

   用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 类。需要在springmvc中进行配置,告诉Spring 该到哪里去找标记为@Controller 的Controller 控制器。

< context:component-scan base-package = "com.host.app.web.controller" >
       < context:exclude-filter type = "annotation"
           expression = "org.springframework.stereotype.Service" />
    </ context:component-scan >

@RequestMapping

  映射 Request 请求与处理器,可以定义在类上,也可以定义在方法上。如果定义在类上,访问的就是相对路径,相对于类而言。如果定义在方法上,相对于方法的映射而言

@Controller
@RequestMapping("/main")
public class MainController {
    
    @RequestMapping(value="index")
    public String index(HttpSession session) {
        session.invalidate();
        return "index";
    }

@RequestParam

当需要从request 中绑定的参数和方法的参数名不相同的时候,也需要在@RequestParam 中明确指出是要绑定哪个参数。在@RequestParam 中除了指定绑定哪个参数的属性value 之外,还有一个属性required ,它表示所指定的参数是否必须在request 属性中存在,默认是true ,表示必须存在,当不存在时就会报错。

@RequestMapping ( "requestParam" )
   public String testRequestParam( @RequestParam(required=false) String name, @RequestParam ( "age" ) int age) {
       return "requestParam" ;
    } 

其他更多详细的注解https://www.cnblogs.com/jpfss/p/8047628.html

 

@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam

@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam

原文链接:http://blog.sina.com.cn/s/blog_6d3c1ec601017q4l.html


下列参数一般都和@RequestMapping配合使用。

 

A@CookieValue

org.springframework.web.bind.annotation.CookieValue

public @interface CookieValue

Annotation which indicates that a method parameter should be bound to an HTTP cookie. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释表示一个方法参数绑定到一个HTTP cookie。支持ServletPortlet环境。

The method parameter may be declared as type Cookie or as cookie value type (String,int,etc).

这个方法的参数可声明为Cookie类型或String,int等。

A.1@CookieValue的属性

String value

The name of the cookie to bind to.

绑定的cookie名称。

boolean required

Whether the header is required.

Default is true,leading to an exception being thrown in case the header is missing in the request. Switch this to false if you prefer a null in case of the missing header.

Head是否需要。默认是true,请求中头丢失将抛出一个异常。False,请求中头丢失将返回null

Alternatively,provide a defaultValue,which implicitly sets this flag to false.

因此,提供一个defaultValue

String defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

requiredfalse,请求中头丢失将返回这个值。

B@PathVariable

Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods in Servlet environments.

    这个参数指出方法的一个参数绑定到一个URI template变量。在Servlet环境中的被@RequestMapping注释的处理器方法。

B.1@PathVariable的属性

value

The URI template variable to bind to.

绑定URI template变量。

举例说明

@Controller

public class HelloWorldController {    @RequestMapping("/helloWorld/{userId}")

public String helloWorld(ModelMap model,@PathVariable("userId") String userId) {

       model.addAttribute("attributeName",userId);

       return "helloWorld";

    }

}

URI template变量和方法的参数名称一样时,可以省略value的定义,@PathVariable达到同样的效果。

 

C@RequestBody

Annotation which indicates that a method parameter should be bound to the web request body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的body。它支持Servlet环境中的注释处理器方法。

class HelloWorldController {

    "/hello.do")   

    public String helloWorld(Model model,100);font-size:10pt;">@RequestBody String reqBody) {

"message",reqBody);

    }

}

这时这个参数reqBody的值是请求页面的form表单的所有值。

 

D@ RequestHeader

Annotation which indicates that a method parameter should be bound to a web request header. Supported for annotated handler methods in Servlet and Portlet environments.

这个注释它指示一个方法的参数绑定到一个web请求的头信息。它支持ServletPortlet环境中的注释处理器方法。

D.1@ RequestHeader的属性

String defaultValue

The default value to use as a fallback.

默认返回值。

Boolean required

Whether the header is required.

是否需要header

String value

The name of the request header to bind to.

绑定的请求头名称。

@RequestHeader("Accept") String info) {

ottom:5px;border:0px;list-style:none;line-height:21px;color:rgb(70, info);

这时这个参数info将获得请求的Accept头信息。

E@RequestParam

org.springframework.web.bind.annotation.RequestParam

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

这个参数指出一个方法的参数应绑定到一个web请求的参数。支持ServletPortlet环境下注释处理器的方法。

E.1@RequestParam的属性

E.1.1value

The name of the request parameter to bind to.

绑定的请求参数的名称。

@RequestParam(value="abc")等同于@RequestParam("abc")

E.1.2required

Whether the parameter is required.

是否需要参数。

Default is true,leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.

默认为true,若请求中没有参数会导致抛出一个异常。若设置为false,若请求中没有参数就会返回null

Alternatively,which implicitly sets this flag to false.

required=false时,最好设置一个defaultValue默认值。

@RequestParam(value = "abc",required=false)

E.1.3defaultValue

The default value to use as a fallback. Supplying a default value implicitly sets required() to false.

required=false时,设定默认值。

"/a")

"/b")

ottom:5px;border:0px;list-style:none;line-height:21px;color:rgb(70,@RequestParam("a") String abc) {

ottom:5px;border:0px;list-style:none;line-height:21px;color:rgb(70,abc);

 

F@ResponseBody

Annotation which indicates that a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.

这个注释它指示一个方法的返回值应该绑定到一个web响应的body中。它支持Servlet环境中的注释处理器方法。

应用@ResponseBody将会跳过视图处理,而是调用合适HttpMessageConverter,将返回值写入输出流。

@ResponseBody

public String helloWorld() {

或者这样定义

"/a/b")

public @ResponseBody String helloWorld() {

这时访问/a/b时,不是返回一个view名为helloWorld的视图,而是作出一个响应,其内容为helloWorld


@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明

@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明

@Param主要是用来注解dao类中方法的参数,在不使用@Param注解的时候,函数的参数只能为一个,并且在查询语句取值时只能用#{},且其所属的类必须为Javabean,而使用@Param注解则可以使用多个参数,在查询语句中使用时可以使用#{}或者${}

 

@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上,http://127.0.0.1:8040/findById/1-->>@GetMapping("/findById/{id}")      参数不能为空

 

@RequestParam注解主要有哪些参数:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

 

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

 

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

 

public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

 

@ResponseBody将响应的结果转为json格式

 

@RequestBody将请求参数转为json格式

@RequestBody 和 @RequestParam(“test”) 的区别与联系

@RequestBody 和 @RequestParam(“test”) 的区别与联系

@RequestBody

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

注:一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam。

注:当同时使用@RequestParam()和@RequestBody时,@RequestParam()指定的参数可以是普通元素、
       数组、集合、对象等等(即:当,@RequestBody 与@RequestParam()可以同时使用时,原SpringMVC接收
       参数的机制不变,只不过RequestBody 接收的是请求体里面的数据;而RequestParam接收的是key-value
       里面的参数,所以它会被切面进行处理从而可以用普通元素、数组、集合、对象等接收)。
       即:如果参数时放在请求体中,传入后台的话,那么后台要用@RequestBody才能接收到;如果不是放在
              请求体中的话,那么后台接收前台传过来的参数时,要用@RequestParam来接收,或则形参前
              什么也不写也能接收。

注:如果参数前写了@RequestParam(xxx),那么前端必须有对应的xxx名字才行(不管其是否有值,当然可以通
       过设置该注解的required属性来调节是否必须传),如果没有xxx名的话,那么请求会出错,报400。

注:如果参数前不写@RequestParam(xxx)的话,那么就前端可以有可以没有对应的xxx名字才行,如果有xxx名
       的话,那么就会自动匹配;没有的话,请求也能正确发送。

原文链接:https://blog.csdn.net/justry_deng/article/details/80972817

今天的关于如何将所有用@RequestParam注释的字段收集到一个对象中的分享已经结束,谢谢您的关注,如果想了解更多关于@Controller和@RequestMapping和@RequestParam注解理解和区别、@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam、@Param @PathVariable @RequestParam @ResponseBody @RequestBody注解说明、@RequestBody 和 @RequestParam(“test”) 的区别与联系的相关知识,请在本站进行查询。

本文标签: