在HTTP中,PUT被定义为idempotent的方法。idempotent(幂等)是一个数学与计算机学概念,常见于抽象代数中。在编程中.一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。
昨日,终于打通了***和***的接口通信,借此机会,我把http put的接口文档定义、接口开发、接口调用整个过程和大家分享一下。
接口文档定义:
1.Released Server:http://***/***;
2.PUT:/***/***;
3.Parameters
Type | Name | Description | Schema | Default |
Header | Authorization required | Basic BASE64(user password) | string |
|
Body | voiceCmdDTO required | voiceCmdDTO | VoiceCmdDTO |
|
VoiceCmdDTO
Type | Description | Schema |
version required | config version | long |
updateTime required | update timestamp in milliseconds | long |
packages required | packages of voice intentions | <VoicePackageDTO>array |
VoicePackageDTO
Type | Description | Schema |
pkg required | package name | string |
versionCode required | package version | long |
data required | config data send to EUI | string |
4.Responses
HttpCode | Description | Schema |
200 | OK | Lele voice intentionss |
400 | Bad request, pkg should not empty and versionCode should greater than 0 | DTO for transfering error message with a list of field errors. |
401 | Authentication required | DTO for transfering error message with a list of field errors. |
- application/json
- application/json
接口开发:
@ApiOperation(value = "Add voice intention commands", notes = "for open platform call, client authentication will be needed."
, response= VoiceCmdDTO.class, code=201)
@ApiResponses({
@ApiResponse(code = 401, message = "Authentication required", response = ErrorDTO.class),
@ApiResponse(code=403, message = "No privilege", response = ErrorDTO.class)
})
@ApiImplicitParam(name="Authorization", value = "Basic BASE64(user password)", dataType = "string", paramType ="header",required = true)
@RequestMapping(value = "/voiceCmd", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<VoiceCmdDTO> voiceCmd(@RequestBody VoiceCmdDTO voiceCmdDTO) throws URISyntaxException {
}
接口调用:
@Component
public class VoiceCmdRequestUtil {
@Value(value = "${voice-cmd.server}")
private String host;
private String detailUrl = "/v1/voiceCmd";
@Value(value = "${voice-cmd.user}")
private String user;
@Value(value = "${voice-cmd.password}")
private String password;
private RestTemplate restTemplate = new RestTemplate();
/**
* 更新VoiceCmdDTO数据到***
* @param voiceCmdDTO RequestBody
* @return HTTP Status Code
*/
public String updateVoiceCmdDTO(VoiceCmdDTO voiceCmdDTO) {
String base64Creds = new String(Base64.encodeBase64((user + password).getBytes()));
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<VoiceCmdDTO> entity = new HttpEntity<VoiceCmdDTO>(voiceCmdDTO, headers);
ResponseEntity<String> response = restTemplate.exchange(host + detailUrl, HttpMethod.PUT, entity, String.class);
return response.getStatusCode().toString();
}
}