http put

      HTTP中,PUT被定义为idempotent的方法。idempotent(幂等)是一个数学与计算机学概念,常见于抽象代数中。在编程中.一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。

     昨日,终于打通了***和***的接口通信,借此机会,我把http put的接口文档定义、接口开发、接口调用整个过程和大家分享一下。

     接口文档定义:

     1.Released Serverhttp://***/***

     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.

     5.Consumes
  •  application/json
     6.Produces
  • 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();
	}
}
     希望我的分享可以帮助进一步了解http put请求。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值