SpringBoot(post、get)中获取客户请求信息、消息头、参数;get请求设置参数,使用场景

1 HttpServletRequest获取客户请求信息、消息头、参数、IP
在方法中获取入参(get方法)

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Controller
public class SpringBootTest {
@RequestMapping(value = "/test", params = {"name","age"},method = RequestMethod.GET)
public @ResponseBody
String getString(HttpServletRequest request) {
    //获取一个参数
    String name=request.getParameter("name");
    //获取所有参数
    Map map=request.getParameterMap();
    return name;
}
}

获取请求方的IP地址:HttpServletRequest还有很多其他方法,获取头信息、cookie、session等等等等

@Controller
@RequestMapping(value ="/test")
public class SpringBootTest {
@RequestMapping(value = "/getIP")
public @ResponseBody String getIP(HttpServletRequest httpServletRequest){
    String ip=httpServletRequest.getRemoteAddr();
    return ip;
}
}

springboot请求将请求参数映射到一个对象上
请求能映射到的原因是实体类Animal 有两个参数name、age,浏览器请求时参数key与属性名一一对应,返回的是json格式

//实体类
import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
}



import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value ="/test")
public class SpringBootTest {
@RequestMapping(value = "/getParam")
public @ResponseBody Animal getParam(Animal animal){
    return animal;
}
}

请求地址为http://127.0.0.1:8080/test/getParam
在这里插入图片描述

请求地址为http://127.0.0.1:8080/test/getParam?name=xx&age=10
在这里插入图片描述

2 注解获取参数get方法

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SpringBootTest {
//路径映射的参数名要与方法中的参数名一致
@RequestMapping(value = "/test", params = {"name","age"},method = RequestMethod.GET)
public @ResponseBody
String getString(@RequestParam String name,@RequestParam Integer age) {
    System.out.println("姓名"+name+"年龄"+age);
    return "姓名"+name+"年龄"+age;
}
}

3 在post方法请求中获取参数,返回String类型
一般post请求才会用到Body
请求为文本格式时,比用HttpServletRequest 的方式方便很多
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SpringBootTest {
@RequestMapping(value = “/test”,method = RequestMethod.POST)
public @ResponseBody
String getString(@RequestBody String body) {
return body;
}
}

请求只要是Spring类型就能获取成功,返回也是String类型,这样就不会强制传json格式的参数文件,也不用写Content-Type:application/json
在这里插入图片描述
4 在post方法请求中获取参数,返回json类型

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller
public class SpringBootTest {
@RequestMapping(value = "/test",method = RequestMethod.POST)
public @ResponseBody
Map getString(@RequestBody Map body) {
    return body;
}
}

请求要用json格式,键值对,返回的也是json格式:方法的返回类型、方法的请求参数类型都要用Map

在这里插入图片描述
post请求要添加头信息
Content-Type=application/json
5 get请求在@RequestParam中设置参数

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SpringBootTest {
//请求参数的表达式只能用“=”或者“!=”不能使用“<”和“>”
@RequestMapping(value = "/test", params = {"userName","age"},method = RequestMethod.GET)
public @ResponseBody
String getString(@RequestParam (value = "name",defaultValue = "test",required = false)String userName,
                 @RequestParam Integer age) {
    return "姓名"+userName+"年龄"+age;
}
}

请求参数userName用@RequestParam注解设置
@RequestParam有3个参数:
1 value:改变该参数在请求时的实际名称,例如这个参数名是userName,用value设置成了name,那么请求时只能用name请求成功,用userName是请求不成功的,这样就不至于将userName暴露在外
2 defaultValue:在该参数是非必填的时候,页面请求时写了该参数,但是没有给值,就会使用defaultValue的值
3 required:设置该参数是否必填,默认为true,设置为false就可以不填写

post请求参数为文本类型,用HttpServiceRequest实现

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Controller
@RequestMapping(value ="/test")
public class SpringBootTest {
@RequestMapping(value = "/getParam",method = RequestMethod.POST)
public @ResponseBody Object getParam(HttpServletRequest request){
    String body=null;
    try {
        /**
         * 用HttpServletRequest类型的参数属性request获取输入流(读)
         * 赋值给BufferedReader类型的bufferedReader(缓冲区读取)读取数据
         * 将获取到的转为json
         **/
        BufferedReader bufferedReader=
                new BufferedReader
                        (new InputStreamReader(request.getInputStream()));
        body=org.apache.commons.io.IOUtils.toString(bufferedReader);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return body;
}
}

请求参数为文本类型的时候就要通过这种输入流获取参数内容,请求的参数非json格式

post请求类型为自定义的实体类

实体类
import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value ="/test")
public class SpringBootTest {
@RequestMapping(value = "/getParam",method = RequestMethod.POST)
public @ResponseBody
Animal getParam(@RequestBody Animal animal) {
    return animal;
}
}

请求的返回类型为自定义的实体类的类型时,实体类的类属性就是请求的参数,并且都是必传
下面这个是没有传sex参数的返回

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6 使用场景
get、post获取请求的参数使用场景
1 两个接口调用时使用
2 从页面请求时使用

@RequestBody注解的3个必须
1 这个注解必须用post方法请求才可以
2 请求参数必须放在body体里
3 请求必须配置Content-Type: application/json

@ResponseBody是做序列化处理的
@RequestBody是做反序列化处理的

猜你喜欢

转载自blog.csdn.net/qq_41767337/article/details/89144733