【问题标题】:Spring Boot store data with WebSocketsSpring Boot 使用 WebSocket 存储数据
【发布时间】:2016-08-02 07:49:19
【问题描述】:

我有一个简单的 WebSocket 设置并尝试保存数据。不知何故,数据没有被持久化。我没有收到任何错误消息,并且该对象被正确返回给客户端。如果我尝试使用 REST 控制器和 REST 请求存储对象,它会起作用。

这是我的 build.gradle 文件的依赖项:

dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-websocket'
compile 'org.springframework:spring-messaging'
compile 'com.google.code.gson:gson:1.7.2'
compile 'org.postgresql:postgresql:9.4-1200-jdbc41'
compile 'commons-dbcp:commons-dbcp:1.4'
testCompile('org.springframework.boot:spring-boot-startet-test')
}

PersonController

@Controller
public class PersonController {

    @Autowired
    PersonRepository personRepository;

    @MessageMapping("/test")
    @SendTo("/response/test")
    public Person test() throws Exception {
        Person person = new Person();
        person.setName("John Doe");
        return personRepository.save(person);
    }
}

STOMP 消息传递配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/response");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket")
                .setAllowedOrigins("*")
                .withSockJS();
    }

个人实体

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return getName;
    }

    public void setName(String name) {
        this.name = name;
    }
}

基础存储库

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {

    void delete(T deleted);

    void delete(ID id);

    Iterable<T> findAll();

    T findOne(ID id);

    T save(T persisted);

    Iterable<T> save(Iterable<T> persited);
}

个人资料库

public interface PersonRepository extends
        BaseRepository<Person, Serializable> {
}
  • 我的代码有问题吗?
  • 缓存有问题吗?我必须强制冲洗吗?
  • SpringBoot 是否支持使用 WebSocket 存储数据?
  • 你知道存储数据的例子吗?我只能找到没有存储数据的基本示例。

【问题讨论】:

  • SpringBoot支持的WebSockets存储数据吗?是的,没有这样的限制,因为我有一个生产应用程序,其中数据从 websocket 上的浏览​​器发送到 Spring Boot 后端,它执行业务逻辑,包括插入、更新等

标签: jpa spring-boot websocket


【解决方案1】:

问题出在我的持久性配置中。我将配置从 Java 实现更改为 application.properties 文件。我认为我的事务管理器有问题。

为了完整,这里是我当前的 application.properties 文件:

spring.datasource.url = jdbc:postgresql://localhost:5432/test
spring.datasource.username = test
spring.datasource.password = test

spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2020-08-05
    • 1970-01-01
    • 2016-05-06
    • 2020-06-17
    • 2015-07-24
    相关资源
    最近更新 更多