平时呢我是很少有写接口的事情的,最近刚接到一个接口的需求,也是一脸懵逼,不知道咋个写,这里记录一下一般性的皆苦规范
一:提供方
1.接口类型REST接口,返回JSON类型数据,请求方式POST [@RestController]
@RequestMapping(path = "/interface/queryCommentPage", method = RequestMethod.POST)
2.用户名密码放在header中
//获取用户名密码
String httpUserName = request.getHeader("userName"); String httpPassWord = request.getHeader("passWord");
3.定义Bean接收参数
public ResultDataInterface queryCommentPage(@RequestBody CommentParam commentParam, HttpServletRequest request){...}
二:调用方
1.postman调用测试
1.1:header中输入权限信息:
1.2:输入业务参数condition对应上面CommentParam 中的一个名为condition的类,属性如下:
1.3:得到结果
2.代码调用
2.1:定义Bean参数QueryCommentParamDTO
public RemoteCommentResultData queryRrmoteCommentDatas(QueryCommentParamDTO queryCommentParamDTO)
2.2:header中放入权限信息
HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); headers.add("AuthorizationName", "我就是你的权限验证信息啊");
2.3:参数,header转JSON
String json = JsonUtil.objectToJson(queryCommentParamDTO); HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
2.4:定义返回值类型RemoteCommentResultData,并调用
import org.springframework.web.client.RestTemplate;
//RemoteCommentResultData返回值类型
res = restTemplate.postForObject(commentpath, formEntity, RemoteCommentResultData.class);