关键词搜索

源码搜索 ×
×

spring boot web api

发布2019-03-28浏览1049次

详情内容

spring boot内置了tomcat,可以很方便的对外提供Web Api。有时实在是很方便,比方说,我写个后台程序,可以处理一些比较耗时的工作。而这个功能,将会开放给WEB前端调用,由WEB前端触发。


2019.08.04
这跟是否内置tomcat应该没有关系吧


用spring boot会很方便。但如果是用C#来弄,后台是后台,还要另外写个WEB程序来提供接口,后台与WEB接口如何交互?鉴于是两个不同的进程,估计需要用到消息中间件。或者是后台这里要支持RPC,远程调用。

言归正传。spring boot项目里,如何提供一个WEB API?

上代码:

@RestController
@RequestMapping(value="/api")
public class ApiController {

	//提交的参数为json
    @RequestMapping(value = "/authors/json/{id}", method = RequestMethod.POST)
    public String test(@PathVariable int id, @RequestParam("t") int type,@RequestBody Author auth){
        return String.format("id:%1$d,type:%2$d,name:%3$s;params type:json",id,type,auth.getName());
    }

	//提交的参数为键值对
    @RequestMapping(value = "/authors/kv/{id}", method = RequestMethod.POST)
    public String test2(@PathVariable int id, @RequestParam("t") int type,Author auth){
        return String.format("id:%1$d,type:%2$d,name:%3$s;params type:key-value",id,type,auth.getName());
    }
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这段代码虽然短小,但蕴含的信息却比较丰富。

1、这是一个POST方式的API,GET的比较简单,不必多说。

2、参数通过3种方式传递:
1)路径,对应{id}

@PathVariable int id
  • 1

2)querystring,对应 ?t=

@RequestParam("t") int type
  • 1

3)提交,对应 Author auth

@RequestBody Author auth
  • 1

@RequestBody 注解表明提交参数形式为json,对应的contentType为application/json

如果不加则为默认的键值对,对应的contentType为application/x-www-form-urlencoded

前端对应访问代码:

//提交键值对
var url1 = "http://localhost:8085/api/authors/kv/1?t=2";
var data1 = "name=chenqu&desc=foolish";
var ctype1 = "application/x-www-form-urlencoded;charset=utf-8";

//提交json
var url2 = "http://localhost:8085/api/authors/json/1?t=2";
var data2 = JSON.stringify({
	"name":"chenqu",
	"desc":"foolish"
});
var ctype2 = "application/json; charset=utf-8";

$.ajax({
	url: url2,
	data: data2,
	contentType: ctype2,		
	//dataType: "json",
	type: "POST",
	success: function (data) {
		alert(data);
	},
	error: function (rq, status, thrown) {
		alert(rq.responseText + "," + status + ": " + thrown);
	}
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载