Connessione a MongoDB in Vaadin con Java, Spring Boot e Flow
In questo articolo vediamo come connetterci a MongoDB in Vaadin e Flow.
Sto usando solo MongoDB, quindi ho levato tutti i riferimenti ad altri db, librerie su Maven comprese.
Questa la dipendenza che dovete aggiungere:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Questa la configurazione nell'application.properties:
server.port=${PORT:8080}
logging.level.org.atmosphere=warn
spring.mustache.check-template-location=false
# Launch the default browser when starting the application in development mode
vaadin.launch-browser=true
# MongoDB configuration
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=testdb
# Vaadin configuration
vaadin.allowed-packages=com.vaadin,org.vaadin,com.test
Questa l'entity:
package com.test.data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Setter
@Getter
@Document(collection = "libri")
public class Libro {
@Id
private String id;
private String titolo;
private List<String> categorie;
}
Il repository:
package com.test.data;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface LibroRepository extends MongoRepository<Libro, String> {
}
Il service:
package com.test.services;
import com.test.data.Libro;
import com.test.data.LibroRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@Service
public class LibroService {
@Autowired
private LibroRepository repository;
public Page<Libro> getAll(Pageable pageable) {
return repository.findAll(pageable);
}
public void add(Libro libro) {
repository.save(libro);
}
}
La view:
package com.test.views.myview;
import com.test.data.Libro;
import com.test.services.LibroService;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.data.VaadinSpringDataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@PageTitle("Libri")
@Route("/")
@Uses(Icon.class)
@Component
public class LibriView extends Composite<VerticalLayout> {
private final LibroService libroService;
public LibriView(LibroService libroService) {
this.libroService = libroService;
Libro libro1 = new Libro();
libro1.setTitolo("IT");
libro1.setCategorie(List.of("Horror", "Fantasy"));
Libro libro2 = new Libro();
libro2.setTitolo("Sahara");
libro2.setCategorie(List.of("Avventura"));
libroService.add(libro1);
libroService.add(libro2);
Grid<Libro> grid = new Grid<>(Libro.class);
getContent().setWidth("100%");
getContent().getStyle().set("flex-grow", "1");
grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
grid.setWidth("100%");
grid.getStyle().set("flex-grow", "0");
grid.setItems(query -> libroService.getAll(VaadinSpringDataHelpers.toSpringPageRequest(query)).stream());
getContent().add(grid);
}
}
Infine l'entry point:
package com.test;
import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.theme.Theme;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Theme(value = "test-vaadin-flow")
public class Application implements AppShellConfigurator {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Enjoy!
java maven vaadin mongodb
Commentami!