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

目前我正在处理一个需要将 SMS 检索功能添加到系统中的项目。我已经使用 spingboot 来构建应用程序。所有的实现都完成了,我已经按照 twillio 上的所有必要配置从客户端检索短信。当我向 Twilio api 发送短信时,它会Unsupported Media Type在调试器中说明。我还向 api 发送了所需的内容类型。当我向 twilio 提供的号码发送短信时会发生这种情况。但是邮递员调用应用程序工作正常。


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

    }

}

http://img1.mukewang.com/6464894800012ed206020593.jpg

芜湖不芜
浏览 133回答 2
2回答

智慧大石

恕我直言,api 调用是最佳选择。RestTemplate请使用如下所示使用已发布的 API 进行实施。public void sendOTP() {&nbsp; &nbsp; RestTemplate restTemplate = new RestTemplate();&nbsp; &nbsp; String message = "Your PIN for account verification is 123456";&nbsp; &nbsp; String user = "******405e4c****19d0******";&nbsp; &nbsp; String password = "******";&nbsp; &nbsp; String smsurl = "https://api.twilio.com/2010-04-01/Accounts/"+user+"/Messages.json";&nbsp; &nbsp; HttpHeaders headers = new HttpHeaders();&nbsp; &nbsp; headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);&nbsp; &nbsp; MultiValueMap<String, String> map = new LinkedMultiValueMap<>();&nbsp; &nbsp; map.add("From", "+1334384****");&nbsp; &nbsp; map.add("To", "+999999999");&nbsp; &nbsp; map.add("Body", message);&nbsp; &nbsp; HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, headers);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(user, password));&nbsp; &nbsp; &nbsp; &nbsp; Object response = restTemplate.postForObject(smsurl, httpEntity, Object.class);&nbsp; &nbsp; &nbsp; &nbsp; LOG.info("Sms Response: {}", gson.toJson(response));&nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; LOG.error(e.getMessage());&nbsp; &nbsp; }}

青春有我

您认为客户端没有指定内容类型。请补充content-type: application/xml。如果你有 spring boot,你可以通过添加以下依赖项来修复它:<dependency>&nbsp; &nbsp; &nbsp;<groupId>com.fasterxml.jackson.dataformat</groupId>&nbsp; &nbsp; &nbsp;<artifactId>jackson-dataformat-xml</artifactId>&nbsp; &nbsp; &nbsp;<version>2.9.8</version></dependency>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java