现在的位置: 首页 > 编程语言 > 正文

springboot前后端传参的实现

2020年02月13日 编程语言 ⁄ 共 4048字 ⁄ 字号 评论关闭

获取传参

@PathVariable注解主要用来获取URL参数。即这种风格的 URL:http://localhost:8080/user/{id}

@GetMapping("/user/{id}") public String testPathVariable(@PathVariable Integer id) { System.out.println("获取到的id为:" + id); return "success"; }

对于多个参数的获取

@GetMapping("/user/{idd}/{name}") public String testPathVariable(@PathVariable(value = "idd") Integer id, @PathVariable String name) { System.out.println("获取到的id为:" + id); System.out.println("获取到的name为:" + name); return "success"; }

@RequestParam:是从 Request 里获取参数值,即这种风格的 URL:http://localhost:8080/user?id=1。除此之外,该注解还可以用于 POST 请求,接收前端表单提交的参数

@RequestMapping("/user") public String testRequestParam(@RequestParam(value = "idd", required = false) Integer id) { System.out.println("获取到的id为:" + id); return "success"; }

当参数较多时,可以不用@RequestParam。而是通过封装实体类来接收参数。

public class User { private String username;private String password;//添加setter和getter }

使用实体接收的话,我们不必在前面加 @RequestParam 注解,直接使用即可。

@PostMapping("/form2") public String testForm(User user) { System.out.println("获取到的username为:" + user.getUsername()); System.out.println("获取到的password为:" + user.getPassword()); return "success"; }

上面的是表单实体提交。当JSON格式提交时,需要用@RequestBody。@RequestBody 注解用于接收前端传来的实体。接收参数为JSON格式的传递。

public class User { private String username; private String password; // set get } @PostMapping("/user") public String testRequestBody(@RequestBody User user) { System.out.println("获取到的username为:" + user.getUsername()); System.out.println("获取到的password为:" + user.getPassword()); return "success"; }

传输时需要传JSON格式的参数。

Restful格式

前后端传参一般使用Restful规范

RESTful 架构一个核心概念是“资源”(Resource)。从 RESTful 的角度看,网络里的任何东西都是资源,可以是一段文本、一张图片、一首歌曲、一种服务等,每个资源都对应一个特定的 URI(统一资源定位符),并用它进行标示,访问这个 URI 就可以获得这个资源。

spring boot的注解很好的支持了restful格式

@GetMapping,处理 Get 请求 @PostMapping,处理 Post 请求 @PutMapping,用于更新资源 @DeleteMapping,处理删除请求 @PatchMapping,用于更新部分资源

@RestController注解可将返回的数据结果转换成json格式,在sprinboot中默认使用的JSON解析技术框架是Jackson。基本示例

@RestController @RequestMapping("/") public class Hello { @GetMapping(value = "hello") public String hello(){ return "hello world"; } }

对null的处理

在项目中,遇到null值时,希望把null都转成""。需要设置一个配置

@Configuration public class Jackson { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); return objectMapper; } }

就会自动把null值转换为空值""

包装统一的JSON返回结构

在后台返回的接口数据中,一般要求结构是统一的,包括有状态码、返回信息。所以可以用泛型封装一个统一的JSON返回结构

public class JsonResult<T> { private T data; private String code; private String msg; /** * 若没有数据返回,默认状态码为0 */ public JsonResult(T data){ this.data = data; this.code = "10200"; this.msg = "操作成功"; } //省略getter和setter

修改controller中的代码

@RequestMapping("/list") public JsonResult<List> getStudentList(){ List<Student> list = new ArrayList<>(); Student stu1 = new Student(2,"22",null); Student stu2 = new Student(3,"33",null); list.add(stu1); list.add(stu2); return new JsonResult<>(list); }

访问url,返回的格式将是:

{"data": [{"id":2,"username":"22","password":""}, {"id":3,"username":"33","password":""}],"code":"10200","msg":"操作成功"}

取配置文件中的值

1、取配置文件中的配置

author.name=zhangsan

可以使用@Value注解即可获取配置文件中的配置信息

@Value("${author.name}") private String userName;

2、设置配置类来保存配置

配置信息如下:

url.orderUrl=http://localhost:8002 url.userUrl=http://localhost:8003 url.shoppingUrl=http://localhost:8004

新建一个配置类,来保存配置

@Component @ConfigurationProperties(prefix = "url") public class MicroServiceUrl { private String orderUrl; private String userUrl; private String shoppingUrl; // 省去 get 和 set 方法 }

@Component 注解是把该类作为组件放在spring容器中,使用时直接注入即可。@ConfigurationProperties注解就是指明该类中的属性名就是配置中去掉前缀后的名字

使用ConfigurationProperties需要加依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>

使用Resource注解就可以将添加配置类MicroServiceUrl引入到controller中使用了

@Resource private MicroServiceUrl microServiceUrl; @GetMapping(value = "getResource") public String getR(){ return microServiceUrl.getUserUrl(); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: spring boot前后端传参的实现

以上就上有关springboot前后端传参的实现的相关介绍,要了解更多spring,boot前后端传参,spring,boot,传参内容请登录学步园。

抱歉!评论已关闭.