devz
devz

Reputation: 2829

Spring Boot store data with WebSockets

I have a simple WebSocket set up and try to save data. Somehow the data gets not persisted. I don't get any error messages and the object gets returned correct to the client. If I try to store the object with a REST controller and a REST request it works.

Here are the dependencies of my build.gradle file:

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

Configuration for STOMP messaging

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

Person entity

@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;
    }
}

Base Repository

@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);
}

Person Repository

public interface PersonRepository extends
        BaseRepository<Person, Serializable> {
}

Upvotes: 0

Views: 3431

Answers (1)

devz
devz

Reputation: 2829

The problem was in my persistence configuration. I changed the configuration from a Java implementation to the application.properties file. I think there was a problem with my transaction manager.

To be complete, here is my current application.properties file:

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

Upvotes: 1

Related Questions