首页 > 其他分享 >spring boot——请求与参数校验——spring-mvc——通过实体类对象获取(推荐)

spring boot——请求与参数校验——spring-mvc——通过实体类对象获取(推荐)

时间:2023-01-15 15:22:36浏览次数:47  
标签:实体类 return String spring boot http public name

通过实体类对象获取(推荐)

我们可以在 Controller 控制器方法的形参中设置一个实体类形参,如果请求参数的参数名与实体类中的属性名一致,那么 Spring MVC 会自动将请求参数封装到该实体类对象中。此时我们就可以通过该实体类对象获取所需的请求参数了。

注:我们推荐大家使用实体类对象来获取请求参数,这也是最常用的请求参数的方式之一,它能够有效地解决“控制器方法形参不适用于请求参数过多的请求”问题。

 

 

 

 

 

 

 

package org.example.entity;

public class Student
{
    private String firstName;
    private String lastName;

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getLastName()
    {
        return lastName;
    }

}

 

 

 

 

 

 

 

 

package org.example.controller.requestparam;

import org.example.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
public class ForParamController
{

    // http://localhost:8080/getRequestParam?name=C语言中文网&url=c.biancheng.net

    /**
     * 通过 HttpServletRequest 获取请求参数
     * @param request
     * @return
     */
    @RequestMapping("/getRequestParam")
    public String requestParam(HttpServletRequest request) {

        String name = request.getParameter("name");
        String url = request.getParameter("url");

        System.out.println("name:" + name);
        System.out.println("url:" + url);


        return "cheng_gong";
    }


    // http://localhost:8080/getRequestParam?firstName=C语言中文网&lastName=c.biancheng.net
    /**
     * 通过 HttpServletRequest 获取请求参数
     * @param request
     * @return
     */
    @RequestMapping(value = "/getRequestParam2")
    public String test2(HttpServletRequest request) {
        String a = request.getParameter("firstName");
        String b = request.getParameter("lastName");
        return a + b;
    }




    //http://localhost:8080/getRequestParam3?name=tom&language=java

    @RequestMapping("/getRequestParam3")
    public String test3(String name, String language) {
        System.out.println("a:" + name);
        System.out.println("b:" + language);
        return "success";
    }



    //  http://localhost:8080/getRequestParam4?name=tom&language=2

    @RequestMapping("/getRequestParam4")
    public String test4(String name, Integer language) {
        System.out.println("a:" + name);
        System.out.println("b:" + language);
        return "success";
    }






    //  http://localhost:8080/testRequestParam5?name=Java&pass=yyds

    @RequestMapping("/testRequestParam5")
    public String testRequestParam(@RequestParam("name") String username, @RequestParam("pass") String password) {
        System.out.println(username + "," + password);
        return "success";
    }







    //    http://localhost:8080/requestParam6?username=Java

    //    http://localhost:8080/requestParam6?username=

    @RequestMapping("/requestParam6")
    public String requestParam22(@RequestParam(value = "username",defaultValue = "abc21") String name)
    {

        return name;
    }



    
    
    // http://localhost:8080/requestParam7?firstName=Java&lastName=python

    @RequestMapping(value = "/requestParam7")
    public String test5(Student student)
    {
        String a = student.getFirstName();
        String b = student.getLastName();
        return a+b;
    }



}

 

 

 

 

 

 

 

 

标签:实体类,return,String,spring,boot,http,public,name
From: https://www.cnblogs.com/xiaobaibailongma/p/17053553.html

相关文章