Skip to content

Instantly share code, notes, and snippets.

View slmanju's full-sized avatar
💭
Whatever you are be a good one

Manjula Jayawardana slmanju

💭
Whatever you are be a good one
View GitHub Profile
@RestController
public class HelloController {
@GetMapping(value = { "/" })
public String index() {
return "Hello world";
}
}
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.*;
@Data
@Table(name = "todos")
@Entity
# H2 console
spring.h2.console.enabled = true
spring.h2.console.path = /h2
# Datasource
spring.datasource.url = jdbc:h2:file:~/todos
spring.datasource.username = sa
spring.datasource.password =
spring.datasource.driver-class-name = org.h2.Driver
spring.jpa.show-sql = true
import com.manjula.todo.model.Todo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TodoRepository extends JpaRepository<Todo, Long> {
}
import lombok.Data;
@Data
public class TodoDto {
private Long id;
private String title;
private String summary;
}
public TodoDto view() {
TodoDto dto = new TodoDto();
BeanUtils.copyProperties(this, dto);
return dto;
}
public static Todo valueOf(TodoDto dto) {
Todo todo = new Todo();
BeanUtils.copyProperties(dto, todo);
return todo;
import com.manjula.todo.dto.TodoDto;
import java.util.List;
public interface TodoService {
Long save(TodoDto todoDto);
TodoDto findById(Long id);
import com.manjula.todo.dto.TodoDto;
import com.manjula.todo.model.Todo;
import com.manjula.todo.repository.TodoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RestController
@RequestMapping("/todos")
public class TodoController {
@Autowired
private TodoService todoService;
@GetMapping
public ResponseEntity<?> getAll() {
// list all
@RestController
@RequestMapping("/todos")
public class TodoController {
@Autowired
private TodoService todoService;
@GetMapping
public ResponseEntity<?> getAll() {
List<TodoDto> todos = todoService.findAll();