vaadin-docs-mcp-server
Version:
MCP server for Vaadin documentation with document-based search and full document retrieval
188 lines (131 loc) ⢠7.64 kB
Markdown
# Vaadin Development Primer (2025+)
**ā ļø Important: Read this document before working with Vaadin to ensure you have an accurate, up-to-date understanding of modern Vaadin development.**
## What is Modern Vaadin?
Vaadin is a **full-stack platform** for building business web applications in Java with **two development models**:
### š Vaadin Flow (Server-Side UI in Java)
- Entire UI built in Java - server-side component model with automatic client-server sync
- Choose when: Java-focused teams, traditional business apps, prefer component-based development
### ā” Vaadin Hilla (React + TypeScript Frontend)
- React/TypeScript UI with type-safe automatic API generation from Java backend
- Choose when: Teams with React expertise, need client-side routing, building public-facing apps
**Key**: Projects typically choose one model. You can mix them, but only when there's a specific need (e.g., offline functionality).
## š Getting Started (The Modern Way)
### Project Creation & Setup
**Use [start.vaadin.com](https://start.vaadin.com)** - generates a "walking skeleton" with production-ready setup including Spring Security, database config, feature-based structure, and working CRUD example.
**Requirements**: Java 17+, Maven (via wrapper), Spring Boot foundation, Node.js (auto-handled)
**Run**: `./mvnw` ā http://localhost:8080
## š Modern Project Structure
Vaadin promotes **feature-based packaging** (not layer-based):
```
src/main/java/
āāā com.example.myapp/
ā āāā Application.java # Spring Boot main class
ā āāā base/ # Shared/reusable code
ā ā āāā domain/
ā ā āāā ui/ (Flow only)
ā āāā security/ # Complete security setup
ā āāā taskmanagement/ # Example feature package
ā āāā domain/ # Entities, repositories
ā āāā service/ # Business logic
ā āāā ui/view/ (Flow only) # UI components
```
### Frontend Structure (Hilla only)
```
src/main/frontend/
āāā components/ # Reusable React components
āāā security/ # Auth context
āāā views/ # Page components
ā āāā @index.tsx # Main page
ā āāā @layout.tsx # Layout wrapper
ā āāā task-list.tsx # Feature views
āāā index.tsx # App entry point
```
## š Built-in Security
**Spring Security** included by default: development mode (in-memory users), production mode (external identity providers), method-level security, type-safe user IDs. Fully customizable.
## š Creating Views
### Flow Views (Java)
Add `@Route("path")` annotation to classes extending Vaadin layouts:
```java
@Route("dashboard")
public class DashboardView extends VerticalLayout {
// View implementation
}
```
### Hilla Views (React)
Use **filesystem-based routing** in `src/main/frontend/views/`:
- `views/dashboard.tsx` ā `/dashboard` route
- `views/@layout.tsx` ā shared layout wrapper
- `views/@index.tsx` ā root `/` route
## š§© Component Ecosystem
**Use Vaadin's comprehensive component library first** before creating custom components. Vaadin includes:
**Data Display & Entry**: Auto CRUD (Hilla only), Auto Grid (Hilla only), Auto Form (Hilla only), Button, Checkbox, Combo Box, Custom Field, Date Picker, Date Time Picker, Email Field, Multi-Select Combo Box, Number Field, Password Field, Radio Button, Select, Text Area, Text Field, Time Picker
**Layouts**: App Layout, Form Layout, Horizontal Layout, Master-Detail Layout, Scroller, Split Layout, Vertical Layout
**Data Visualization**: Charts (multiple types - search for specific chart components using provided tools), Dashboard, Grid, Grid Pro, Tree Grid, Virtual List
**Navigation & UI**: Accordion, Avatar, Badge, Context Menu, Details, Dialog, Icons, List Box, Menu Bar, Notification, Popover, Side Navigation, Tabs, Tooltip
**Advanced**: Board, Card, Confirm Dialog, Cookie Consent, CRUD, Login, Map, Markdown, Message Input, Message List, Progress Bar, Rich Text Editor, Spreadsheet, Upload
**Approach**: Compose existing components and layouts before building custom ones from scratch.
## šļø Architecture Principles
**Feature-Based Organization**: Each feature in its own package (domain, service, ui). Use `@Service` + `@Transactional` + security annotations for business logic.
**Hilla Type Safety**: `@BrowserCallable` services auto-generate TypeScript APIs with runtime validation.
## š Hilla @BrowserCallable Endpoints
Hilla's key feature is **type-safe communication** between React frontend and Java backend through `@BrowserCallable` endpoints.
### Defining Endpoints in Java
```java
@BrowserCallable
@Service
public class TaskEndpoint {
private final TaskService taskService;
// Simple method with validation
public Task createTask(@Valid CreateTaskRequest request) {
return taskService.createTask(request);
}
// Method that can throw exceptions
public void deleteTask(UUID taskId) throws TaskNotFoundException {
taskService.deleteTask(taskId);
}
}
```
### DTO Classes (Automatically Converted to TypeScript)
```java
public record CreateTaskRequest(
@NotBlank String title,
@Size(max = 500) String description
) {}
```
### Package-level Null Safety
Create `package-info.java` in your endpoint package to avoid repetitive `@NotNull`:
```java
@org.springframework.lang.NonNullApi
package com.example.endpoints;
```
### Calling from TypeScript
After running `./mvnw compile vaadin:generate`, you get type-safe TypeScript clients:
```typescript
import { TaskEndpoint } from 'Frontend/generated/endpoints';
import type { CreateTaskRequest } from 'Frontend/generated/com/example/data';
const createTask = async (request: CreateTaskRequest) => {
try {
// Type-safe call with automatic validation
const newTask = await TaskEndpoint.createTask(request);
console.log('Created:', newTask);
} catch (error) {
console.error('Validation or business logic error:', error);
}
};
```
### Key Benefits & Best Practices
**ā
Full Stack Type Safety:** Java DTOs automatically become TypeScript interfaces with preserved method signatures and validation.
**šÆ Best Practices:**
- Use **record classes** for DTOs and **Bean Validation** annotations
- Add `@NonNullApi` to `package-info.java` to avoid repetitive `@NotNull`
- Keep endpoints **stateless** - delegate to injected services
- Generate API after changes: `./mvnw compile vaadin:generate`
**ā ļø Important:** Only `@BrowserCallable` methods are exposed. Security annotations (`@RolesAllowed`, etc.) work on endpoints.
## š¦ Key Dependencies
**Core**: `vaadin-spring-boot-starter` dependency + `vaadin-bom` for version management
**Maven Plugin**: `vaadin-maven-plugin` handles frontend resources, optimization, TypeScript compilation, API generation (`vaadin:generate` for Hilla)
## š Deployment & Production
**Deployment**: Executable JAR (recommended), Docker containers, any Java-compatible cloud platform
**Production Build**: `./mvnw clean package -Pproduction` (creates optimized bundles, minification, production profiles)
---
**Important**: When working on existing projects, stick to the existing patterns in the project. For new projects or when users ask for architectural guidance, recommend the feature-based package structure described above.