springboot Restful风格post请求接口

您所在的位置:网站首页 精工原装电池多少钱 springboot Restful风格post请求接口

springboot Restful风格post请求接口

2023-09-05 12:46| 来源: 网络整理| 查看: 265

springboot Restful风格post请求接口 一、接收 Form 表单数据 1.基本的接收方法

package com.example.demo.controller; import com.example.demo.model.Phone; import com.example.demo.model.User; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.List; import java.util.Map; /** * RESTful风格POST请求接口 * */ @RestController public class RestfulPostController { /** * POST 方式传递过来的参数 * 接收 form-data 格式的 POST 数据: *http://127.0.0.1:8080/postNameAge * */ @PostMapping("/postNameAge") public String hello(@RequestParam("name") String name, @RequestParam("age") Integer age) { return "name:" + name + "\nage:" + age; } }

在这里插入图片描述

2,参数没有传递的情况

/** * POST 方式传递过来的参数 * 如果没有传递参数 Controller 将会报错,这个同样有如下两种解决办法: * 使用 required = false 标注参数是非必须的。 * 使用 defaultValue 给参数指定个默认值。 *http://127.0.0.1:8080/postName * */ @PostMapping("/postName") public String postName(@RequestParam(name = "name", defaultValue = "springboot") String name, @RequestParam(name = "age", required = false) Integer age) { return "name:" + name + "\nage:" + age; }

在这里插入图片描述

3,使用 map 来接收参数

/** * POST 方式传递过来的参数 * 使用 map 来接收参数 *http://127.0.0.1:8080/postMap * */ @PostMapping("/postMap") public String postMap(@RequestParam Map params) { return "name:" + params.get("name") + "\nage:" + params.get("age"); }

在这里插入图片描述

4,接收一个数组

/** * POST 方式传递过来的参数 * 接收一个数组 *http://127.0.0.1:8080/postArray * */ @PostMapping("/postArray") public String postArray(@RequestParam("name") String[] names) { String result = ""; for(String name:names){ result += name + "\n"; } return result; }

在这里插入图片描述

5,使用对象来接收参数 (1.)接收单个对象

package com.example.demo.model; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } /** * POST 方式传递过来的参数 * 使用对象来接收参数 *http://127.0.0.1:8080/postUser * */ @PostMapping("/postUser") public String postUser(User user) { return "name:" + user.getName() + "\nage:" + user.getAge(); }

在这里插入图片描述

在这里插入图片描述

(2.)接收多个对象

package com.example.demo.model; public class Phone { private String number; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } /** * POST 方式传递过来的参数 * 使用多个对象来接收参数 *http://127.0.0.1:8080/postUserPhone * */ @PostMapping("/postUserPhone") public String postUserPhone(User user, Phone phone) { return "name:" + user.getName() + "\nage:" + user.getAge() + "\nnumber:" + phone.getNumber(); }

在这里插入图片描述 6,使用对象接收时指定参数前缀 如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递: 我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u. 在这里插入图片描述

/** * POST 方式传递过来的参数 * 使用对象接收时指定参数前缀 * 如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递: * 我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u. *http://127.0.0.1:8080/postU * */ @PostMapping("/postU") public String postU(@ModelAttribute("u") User user) { return "name:" + user.getName() + "\nage:" + user.getAge(); } @InitBinder("u") private void initBinder(WebDataBinder binder) { binder.setFieldDefaultPrefix("u."); }

在这里插入图片描述 二、接收字符串文本数据 (1)如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。

/** * POST 方式传递过来的参数 * 如果传递过来的是 Text 文本,我们可以通过 HttpServletRequest 获取输入流从而读取文本内容。 *http://127.0.0.1:8080/postText * */ @PostMapping("/postText") public String postText(HttpServletRequest request) { ServletInputStream is = null; try { is = request.getInputStream(); StringBuilder sb = new StringBuilder(); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { sb.append(new String(buf, 0, len)); } System.out.println(sb.toString()); return "获取到的文本内容为:" + sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; }

在这里插入图片描述 三、接收 JSON 数据 1,使用 Map 来接收数据 如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map:

/** * POST 方式传递过来的参数 * 接收 JSON 数据 * 使用 Map 来接收数据 * 如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据转换 Map: *http://127.0.0.1:8080/postRequestMap * */ @PostMapping("/postRequestMap") public String postRequestMap(@RequestBody Map params) { return "name:" + params.get("name") + "\n age:" + params.get("age"); }

在这里插入图片描述 2,使用 Bean 对象来接收数据 (1.)如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据直接转换成对象:

package com.example.demo.model; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } /** * POST 方式传递过来的参数 * 接收 JSON 数据 * 使用 Bean 对象来接收数据 * 如果把 json 作为参数传递,我们可以使用 @requestbody 接收参数,将数据直接转换成对象: *http://127.0.0.1:8080/postRequestBean * */ @PostMapping("/postRequestBean") public String postRequestBean(@RequestBody User user){ return user.getName() + " " + user.getAge(); }

在这里插入图片描述 (2)如果传递的 JOSN 数据是一个数组也是可以的,Controller 如下

/** * POST 方式传递过来的参数 * 接收 JSON 数据 * JOSN 数据是一个数组 *http://127.0.0.1:8080/postListUser * */ @PostMapping("/postListUser") public String hello(@RequestBody List users){ String result = ""; for(User user:users){ result += user.getName() + " " + user.getAge() + "\n"; } return result; }

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3