一般性接口開發規范


平時呢我是很少有寫接口的事情的,最近剛接到一個接口的需求,也是一臉懵逼,不知道咋個寫,這里記錄一下一般性的皆苦規范

一:提供方

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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM