TransWikia.com

Подключение к сервису

Stack Overflow на русском Asked on December 16, 2021

Создал свой REST сервер и Deploy его на heroku, создал на нем websocket

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
 
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }
 
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/message").withSockJS();
    }
    @MessageMapping("/hello")
    @SendTo("/topic/greeting")
    public Message greetings(Message message) {
        System.out.println("websocket");
         return  messageRepo.save(message);
    }
@Entity
public class Message {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
 
    private String name;
    private String text;
 
    @Override
    public String toString() {
        return "Message{" +
                "Id=" + id +
                ", name='" + name + ''' +
                ", text='" + text + ''' +
                '}';
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setText(String text) {
        this.text = text;
    }
 
    public String getName() {
        return name;
    }
 
    public String getText() {
        return text;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    public Long getId(){
        return id;
    }
}

Так же создал на стороне отдельное приложение в idea, что бы протестировать его работу

public void connect() throws InterruptedException {
 
    Request request = new Request.Builder()
            .url("wss://myserver-ioan.herokuapp.com/message")
            .build();
 
    OkHttpClient client = new OkHttpClient();
    WebSocket ws = client.newWebSocket(request, new EchoWebSocketListener());
 
 
 
    client.dispatcher().executorService().shutdown();
 
    }
class EchoWebSocketListener extends WebSocketListener {
    private static final int NORMAL_CLOSURE_STATUS = 1000;
    private JsonAdapter<Message> adapter;
 
    EchoWebSocketListener()
    {
        Moshi moshi = new Moshi.Builder().build();
        adapter = moshi.adapter(Message.class);
    }
 
    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        super.onOpen(webSocket, response);
        System.out.println("open");
    }
 
 
    @Override
    public void onMessage(WebSocket webSocket, String text) {
            System.out.println("onMessage " + text);
    }
 
    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        webSocket.close(NORMAL_CLOSURE_STATUS, null);
        System.out.println("on Close ");
    }
    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        System.out.println("on Failure " + t.getMessage() + " " + response.toString());
    }
}

Но при попытке подключится вылазит постоянно ошибка

on Failure Expected HTTP 101 response but was '500 ' Response{protocol=http/1.1, code=500, message=, url=https://myserver-ioan.herokuapp.com/message}

И сам сайт говорит

There was an unexpected error (type=Internal Server Error, status=500).
Failed to convert value of type 'java.lang.String' to required type 'com.io.messages.domain.Message'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.io.messages.domain.Message': no matching editors or conversion strategy found

Облазил кучу сайтов и вроде бы все делал пр туториалам, но все равно ничего не работает, в чем проблема?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP