当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot前后端传参的实现

spring boot前后端传参的实现

2020年03月09日  | 移动技术网IT编程  | 我要评论

获取传参

@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(); 
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网