Return Twilio SMS 不支持内容类型 api
Return content type not supported from Twilio SMS api
我目前正在处理一个需要向系统添加 SMS 检索功能的项目。我已经使用 spingboot 来构建 application.All 完成的实现,并且我已经按照 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());
}
}
您认为客户端没有指定内容类型。请添加 content-type: application/xml
.
如果您有 spring 引导,您可以通过添加以下依赖项来修复它:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
恕我直言,api 打电话是最好的选择。请使用已发布的 API 使用 RestTemplate
进行实施,如下所示。
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());
}
}
我目前正在处理一个需要向系统添加 SMS 检索功能的项目。我已经使用 spingboot 来构建 application.All 完成的实现,并且我已经按照 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());
}
}
您认为客户端没有指定内容类型。请添加 content-type: application/xml
.
如果您有 spring 引导,您可以通过添加以下依赖项来修复它:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
恕我直言,api 打电话是最好的选择。请使用已发布的 API 使用 RestTemplate
进行实施,如下所示。
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());
}
}