package com.twilio.websocketcallback.domain;
public class SMS { private String to; private String message;
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "SMS{" +
"to='" + to + '\'' +
", message='" + message + '\'' +
'}';
}
} Criar um controlador de gerenciamento de mensagens Na abordagem do Spring para trabalhar com mensagens STOMP, as mensagens STOMP podem ser roteadas para classes @Controller. Por exemplo, SMSController eฬ mapeado para lidar com mensagens para o destino /sms.
package com.example.websocketcallback;
import com.twilio.exception.ApiException; import com.twilio.websocketcallback.domain.SMS; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;
@RestController public class SMSController {
@Autowired
SMSService service;
@Autowired
private SimpMessagingTemplate webSocket;
private final String TOPIC_DESTINATION = "/topic/sms";
@RequestMapping(value = "/sms", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void smsSubmit(@RequestBody SMS sms) {
try{
service.send(sms);
}
catch(ApiException e){
webSocket.convertAndSend(TOPIC_DESTINATION, getTimeStamp() + ": Error sending the SMS: "+e.getMessage());
throw e;
}
webSocket.convertAndSend(TOPIC_DESTINATION, getTimeStamp() + ": SMS has been sent!: "+sms.getTo());
}
@RequestMapping(value = "/smscallback", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void smsCallback(@RequestBody MultiValueMap<String, String> map) {
service.receive(map);
webSocket.convertAndSend(TOPIC_DESTINATION, getTimeStamp() + ": Twilio has made a callback request! Here are the contents: "+map.toString());
}
private String getTimeStamp() {
return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
}
}