java - Twilio SMS api 不支持返回内容类型

标签 java spring spring-boot twilio twilio-api

目前我正在开发一个项目,需要将短信检索功能添加到系统中。我已经使用 springboot 来构建应用程序。所有实现都已完成,并且我已遵循 twillio 上的所有必要配置来从客户端检索短信。当我向 Twilio api 发送短信时,它在调试器中指出不支持的媒体类型。我还将所需的内容类型发送到 api。 当我向 twilio 提供的号码发送短信时,会发生这种情况。但是 postman 对应用程序的调用工作正常

package com.crustykrabs.application.service;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static spark.Spark.*;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.messaging.Body;
import com.twilio.twiml.messaging.Message;

@RestController
public class TextMessageController {

    @PostMapping(path = "/messages/textmessages/receive", consumes = "application/xml", produces = "application/xml")
    public @ResponseBody ResponseEntity<String> receive() {
            Body body = new Body
                    .Builder("The Robots are coming! Head for the hills!")
                    .build();
            Message sms = new Message
                    .Builder()
                    .body(body)
                    .build();
            MessagingResponse twiml = new MessagingResponse
                    .Builder()
                    .message(sms)
                    .build();
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body(twiml.toXml());
    }
}

enter image description here

最佳答案

恕我直言,API 调用是最好的选择。请使用 RestTemplate 来实现已发布的 API,如下所示。

public void sendOTP() {
    RestTemplate restTemplate = new RestTemplate();

    String message = "Your PIN for account verification is 123456";
    String user = "******405e4c****19d0******";
    String password = "******";
    String smsurl = "https://api.twilio.com/2010-04-01/Accounts/"+user+"/Messages.json";

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("From", "+1334384****");
    map.add("To", "+999999999");
    map.add("Body", message);

    HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, headers);

    try {
        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));
        Object response = restTemplate.postForObject(smsurl, httpEntity, Object.class);
        LOG.info("Sms Response: {}", gson.toJson(response));
    } catch(Exception e) {
        LOG.error(e.getMessage());
    }
}

关于java - Twilio SMS api 不支持返回内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353488/

相关文章:

java - 使用 Java 创建并连接到 SQL 数据库

java - 是否可以在@RabbitListener 上设置预取计数

java - 在不同的类中创建 Camel 路线

java - 如何使用 :list behave with scope ="prototype"?

java - 将 application.properties 传递给 Logback 自定义过滤器

java - 尝试检索字段值时出现 NoSuchFieldException

java - Jackson 数据不一致的编码

java - 找不到类型为 : class org. json.JSONObject 的返回值的转换器

java - 布隆过滤器用于在 O(n) 内从整数流中删除重复项

java - Axon MongoDB - 消息 ='E11000 duplicate key error collection uniqueAggregateIndex dup key: { : "101",: 0 }