Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有基本身份验证和自定义对象的Spring引导Resttemplate HTTP POST_Java_Rest_Spring Boot_Http Headers - Fatal编程技术网

Java 带有基本身份验证和自定义对象的Spring引导Resttemplate HTTP POST

Java 带有基本身份验证和自定义对象的Spring引导Resttemplate HTTP POST,java,rest,spring-boot,http-headers,Java,Rest,Spring Boot,Http Headers,我有一个spring boot REST服务,我想通过以下方式对其进行HTTP POST调用: 1-基本身份验证 2-将我的对象作为实体发送 非常感谢您的帮助 到目前为止我的代码 public String envoyer(SMS sms) { logger.info("Envoie SMS au serveur samba sur {} : {} : {}", properties.getUser(), properties.getPassword(), properties.

我有一个spring boot REST服务,我想通过以下方式对其进行HTTP POST调用: 1-基本身份验证 2-将我的对象作为实体发送

非常感谢您的帮助

到目前为止我的代码

public String envoyer(SMS sms) {
        logger.info("Envoie SMS au serveur samba sur {} : {} : {}", properties.getUser(), properties.getPassword(), properties.getUrl());
        logger.info("SMS : {}", sms.toString());
        HttpEntity<String> request = getRequest(sms);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(properties.getUrl(), sms, String.class);
        return responseEntity.getBody();
    }

    private String getBase64Credentials(){
        Base64.Encoder encoder = Base64.getEncoder();
        String creds = properties.getUser()+":"+properties.getPassword();
        byte[] credBytes = creds.getBytes();
        byte[] base64Bytes = encoder.encode(credBytes);
        logger.info("Credentials === "+creds);
        logger.info("Credentials Bytes === "+new String(credBytes));
        logger.info("Encoded Credentials === "+new String(base64Bytes));
        return new String(base64Bytes);
    }

    private HttpEntity getRequest(SMS sms){

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + getBase64Credentials());
        headers.add("sms", sms.toString());
        return new HttpEntity<>(headers);
    }
公共字符串特使(SMS){
info(“Envoie SMS au serveur samba sur{}:{}:{}”,properties.getUser(),properties.getPassword(),properties.getUrl());
info(“SMS:{}”,SMS.toString());
HttpEntity请求=getRequest(sms);
ResponseEntity ResponseEntity=restTemplate.postForEntity(properties.getUrl(),sms,String.class);
返回responseEntity.getBody();
}
私有字符串getBase64Credentials(){
Base64.Encoder编码器=Base64.getEncoder();
字符串creds=properties.getUser()+“:”+properties.getPassword();
字节[]credBytes=creds.getBytes();
字节[]base64Bytes=编码器.encode(credBytes);
logger.info(“凭证==”+凭证);
info(“凭据字节==”+新字符串(credBytes));
info(“编码凭证==”+新字符串(base64字节));
返回新字符串(base64字节);
}
私有HttpEntity getRequest(SMS){
HttpHeaders=新的HttpHeaders();
添加(“授权”、“基本”+getBase64Credentials());
headers.add(“sms”,sms.toString());
返回新的HttpEntity(标头);
}
找到了答案

与其使用HttpEntity,不如使用


RequestEntity req=newrequestEntity(smsString,headers,HttpMethod.POST,URI.create(url),String.class)

到目前为止你得到了什么?请看编辑@丹尼尔斯奎尔斯