Spring Cloud注解的使用

本文探讨了SpringBoot框架下RESTful API的设计规范,包括使用RequestParam与RequestBody注解的区别,以及如何通过GetMapping和PostMapping等注解来指定HTTP请求方法。同时,详细解释了如何设置produces属性来确保API返回JSON格式数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:编程规范的重要性不言而喻,之前我们接口的传参都是使用Map的形式,随着公司的发展,以及慢慢的规范的建立,我们要求传参的时候尽量使用对象作为入参,Spring boot强制返回的的数据是JSON形式的
  1. RequestParam与RequestBody:RequestParam注解接收的参数是来自于request header中,即请求头,也就是在url中,格式为xxx?username=123&password=456,而RequestBody注解接收的参数则是来自于requestBody中,即请求体中。包含RequestBody的接口,前端传递的request header Content-Type必须是JSON串。
    如果接口是如下的形式
@RequestMapping(value = "/getMemberList")
public BaseResponseObject getMemberList(@RequestBody SearchMember searchMember) {
	BaseResponseObject baseResponseObject = new BaseResponseObject(Boolean.FALSE, "", "");
}

前端调用的时候只能使用POST的请求方式

{"visitSiteId":25,"firstResult":0,"maxResult":20,"sortType":2,"memberName":"pppp ppp"}

因为这是一个获取列表的请求,一般我们使用的是GET请求,但是GET请求无法传递JSON对象,只能传递键值对的JSON字符串,为了兼容之前的,我只能做个修改

@RequestMapping(value = "/getMemberList")
public BaseResponseObject getMemberList(SearchMember searchMember) {
	BaseResponseObject baseResponseObject = new BaseResponseObject(Boolean.FALSE, "", "");
}

这样前端调用的时候可以使用/getMemberList?visitSiteId=24&firstResult=0&maxResult=20&memberName=pppp ppp

  1. 关于RequestMapping、PostMapping、GetMapping,后续两个是第一个简写的形式,平时用RequestMapping喜欢@RequestMapping(value = "/getNodeMapList", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON),如果使用GetMapping就可以像这样@GetMapping(value = "/getNodeMapList",produces = MediaType.APPLICATION_JSON )
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {

	@AliasFor(annotation = RequestMapping.class)
	String name() default "";
	@AliasFor(annotation = RequestMapping.class)
	String[] value() default {};
	@AliasFor(annotation = RequestMapping.class)
	String[] path() default {};
	@AliasFor(annotation = RequestMapping.class)
	String[] params() default {};
	@AliasFor(annotation = RequestMapping.class)
	String[] headers() default {};
	@AliasFor(annotation = RequestMapping.class)
	String[] consumes() default {};
	@AliasFor(annotation = RequestMapping.class)
	String[] produces() default {};

}

PS:注意到我在RequestMapping和GetMapping都设定了produces的值,这里的目的是为了把返回值的类型定义为JSON,就是在response header中加上Content-Type: application/json;charset=UTF-8,如果没有这个值的话,默认是Content-Type: application/xhtml+xml;charset=UTF-8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值