Java -- 如何编写一个接收 GET / POST请求的 Rest 接口 & 实例分析

本文详细介绍了如何在Java中编写REST接口,包括Controller类的定义,使用@PathVariable、@RequestParam和@RequestBody处理GET和POST请求。通过不同方式的实例分析,展示了如何接收和响应HTTP请求,提供了各种请求参数的处理方法及其返回结果。
摘要由CSDN通过智能技术生成

接口类定义

Controller 定义

@Controller("test")
@RequestMapping(value = "/api/rest/test")
@Api(value = "test", description = "测试相关接口", basePath = "/api/rest/test", position = 1, produces = "application/json")
public class ResourceController extends Abstract***Controller{
   
	
}

引用包

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiParam;

Get 请求

@PathVariable

方式一

访问 url:

http://ip:8080/应用名/service/api/rest/test/alertsInfo/1
@RequestMapping(value = "/alertsInfo/{resId:.+}", method = RequestMethod.GET)
@ResponseBody
public List<JSONObject> getAlertsByResId(
		@ApiParam(required = true, name = "resId", value = "资源ID") @PathVariable String resId) {
   
		
	JSONObject obj1 = new JSONObject();
	obj1.put("resId", resId);
	obj1.put("alertContent", "test1");
	JSONObject obj2 = new JSONObject();
	obj2.put("resId"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值