当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot构建Restful service完成Get和Post请求

SpringBoot构建Restful service完成Get和Post请求

2019年07月19日  | 移动技术网IT编程  | 我要评论

一个基本的restfule service最进场向外提供的请求method就是get和post。

在get中,常用的都会在请求上带上参数,或者是路径参数。响应json。

在post中,常用的会提交form data或者json data作为参数,响应json。

1. get请求,url传参,返回json。

先准备一个请求后,响应的对象。

package com.example.demo;
public class echo {
  private final long id;
  private final string content;
  public echo(long id, string content) {
    this.id = id;
    this.content = content;
  }
  public long getid() {
    return this.id;
  }
  public string getcontent() {
    return this.content;
  }
}

准备一个用来接收请求的echocontroller(名字可以根据实际情况写),为它增加@restcontroller注解,表示这是一个处理restful请求的响处理类。

增加@requestmapping,为本controller提供一个根url,当然可以不写,写这个是为了更好的将同一种处理的url归在一类,本controller其他的处理方法对应的url中就不需要重复写。

package com.example.demo;
import java.util.concurrent.atomic.atomiclong;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.bind.annotation.requestbody;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.modelattribute;
@restcontroller
@requestmapping("/echo")
public class echocontroller {
  private static final string echotemplate1 = "received %s!";
  private static final string echotemplate2 = "%s speak to %s \'%s\'";
  private final atomiclong counter = new atomiclong();
  @requestmapping(value="/getter/pattern1", method=requestmethod.get)
  public echo getterpattern1(string content) {
    return new echo(counter.incrementandget(), string.format(echotemplate1, content));
  }
  @requestmapping(value="/getter/pattern2", method=requestmethod.get)
  public echo getterpattern2(@requestparam(value="content", required=false) string alias) {
    return new echo(counter.incrementandget(), string.format(echotemplate1, alias));
  }
}

getterpattern1的上面增加了@requestmapping注解,将指定的url和处理的方法进行映射,对应这个url的请求就由该方法来处理,method可以省略,省略后就是对应所有的http metho,gtterpatten1方法的参数默认就和url中的content参数进行映射。

再看getterpattern2,跟上面的方法效果一致,他们的区别就在于参数定义用了@requestparam注解将url参数和方法参数进行了映射说明,@requesteparam中value的值就是url中实际的参数,required说明该参数是否必须,如果是true,而实际上url中并没有带上该参数,那么会报异常,为防止异常可以增加defaultvalue指定参数的默认值即可。我们会发现这里的方法参数是alias,这里当然是可以和url的参数名不同,只要requestparam注解映射了他们的关系就没问题。

运行后,可以在浏览器中访问对应的url来看看结果,我这里是用curl来访问。

curl
curl

 上面两个url的访问得到的结果除了id会自增外,其他是一致的:

{"id":6,"content":"received hello!"} 

 2. get请求,传递url路径参数,返回json。

在echocontroller中增加一个响应方法。

  @requestmapping(value="/getter/pattern3/{content}", method=requestmethod.get)
  public echo getterpattern3(@pathvariable string content) {
    return new echo(counter.incrementandget(), string.format(echotemplate1, content));
  }

可以看到,在@requestmapping的url定义中的末尾有“{content}”,表明这里是一个路径参数,在getterpattern3的参数content增加了@pathvariable注解,将方法参数与路径参数进行了映射。

运行后,访问url。

curl

结果:

{"id":8,"content":"received 123456!"}

3.post请求,参数以http body的途径提交json数据。

先定义一个提交的json对应的对象,这里把它定义为message。

package com.example.demo;
public class message {
  private string from;
  private string to;
  private string content;
  public message() {}
  public string getfrom() {
    return this.from;
  }
  public string getto() {
    return this.to;
  }
  public string getcontent() {
    return this.content;
  }
  public void setfrom(string value) {
    this.from = value;
  }
  public void setto(string value) {
    this.to = value;
  }
  public void setcontent(string value) {
    this.content = value;
  }
}

在echocontroller增加响应的方法,并完成映射。

  @requestmapping(value="/setter/message1", method=requestmethod.post)
  public echo settermessage1(@requestbody message message) {
    return new echo(counter.incrementandget(), string.format(echotemplate2, message.getfrom(), message.getto(), message.getcontent()));
  }

在settermessage1方法的参数中用@requestbody将请求的http body和参数messge进行了映射。

运行后,使用curl向服务端提交json数据。提交的请求头部要带上"content-type:application/json",表明请求体json。

curl -i -h "content-type:application/json" -d "{\"from\":\"tom\",\"to\":\"sandy\",\"content\":\"hello buddy\"}" http://localhost:8080/echo/setter/message1

结果:

{"id":9,"content":"tom speak to sandy 'hello buddy'"}

4.post请求,参数以http body的途径提交表单数据。

在echocontroller增加响应的方法,并完成映射。

  @requestmapping(value="/setter/message2", method=requestmethod.post)
  public echo settermessage2(@modelattribute message message) {
    return new echo(counter.incrementandget(), string.format(echotemplate2, message.getfrom(), message.getto(), message.getcontent()));
  }

 在settermessage2方法的参数中用@modelattribute将请求的http body中的表单数据和参数messge进行了映射。

运行后,使用curl向服务端提交表单数据。提交的请求头部要带上"content-type:application/x-www-form-urlencoded",表明请求体是表单数据,格式是"key1=value1&key2=value2&key3=value3"。

curl -i -h "content-type:application/x-www-form-urlencoded" -d "from=sandy&to=aissen&content=go to" http://localhost:8080/echo/setter/message2

 结果:

{"id":11,"content":"sandy speak to aissen 'go to'"}

总结

以上所述是小编给大家介绍的springboot构建restful service完成get和post请求,希望对大家有所帮助

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

相关文章:

验证码:
移动技术网