on
Italy
- Get link
- X
- Other Apps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| 1.8 |
UTF- 8 class>com.javaadvent.bootrest.TodoAppConfigclass> org.springframework.boot spring-boot-starter-parent 1.1.9.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.data spring-data-mongodb org.springframework.boot spring-boot-maven-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| package com.javaadvent.bootrest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfiguration@ComponentScanpublic class TodoAppConfig { public static void main(String[] args) { SpringApplication.run(TodoAppConfig.class, args); }} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
| import org.springframework.data.annotation.Id;import static com.javaadvent.bootrest.util.PreCondition.isTrue;import static com.javaadvent.bootrest.util.PreCondition.notEmpty;import static com.javaadvent.bootrest.util.PreCondition.notNull;final class Todo { static final int MAX_LENGTH_DESCRIPTION = 500; static final int MAX_LENGTH_TITLE = 100; @Id private String id; private String description; private String title; public Todo() {} private Todo(Builder builder) { this.description = builder.description; this.title = builder.title; } static Builder getBuilder() { return new Builder(); } //Other getters are omitted public void update(String title, String description) { checkTitleAndDescription(title, description); this.title = title; this.description = description; } /** * We don't have to use the builder pattern here because the constructed * class has only two String fields. However, I use the builder pattern * in this example because it makes the code a bit easier to read. */ static class Builder { private String description; private String title; private Builder() {} Builder description(String description) { this.description = description; return this; } Builder title(String title) { this.title = title; return this; } Todo build() { Todo build = new Todo(this); build.checkTitleAndDescription(build.getTitle(), build.getDescription()); return build; } } private void checkTitleAndDescription(String title, String description) { notNull(title, "Title cannot be null"); notEmpty(title, "Title cannot be empty"); isTrue(title.length() <= MAX_LENGTH_TITLE, "Title cannot be longer than %d characters", MAX_LENGTH_TITLE ); if (description != null) { isTrue(description.length() <= MAX_LENGTH_DESCRIPTION, "Description cannot be longer than %d characters", MAX_LENGTH_DESCRIPTION ); } }} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| import org.springframework.data.repository.Repository;import java.util.List;import java.util.Optional;interface TodoRepository extends Repository void delete(Todo deleted); List Optional Todo save(Todo saved);} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| import java.util.List;interface TodoService { TodoDTO create(TodoDTO todo); TodoDTO delete(String id); List TodoDTO findById(String id); TodoDTO update(TodoDTO todo);} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;import java.util.Optional;import static java.util.stream.Collectors.toList;@Servicefinal class MongoDBTodoService implements TodoService { private final TodoRepository repository; @Autowired MongoDBTodoService(TodoRepository repository) { this.repository = repository; } @Override public TodoDTO create(TodoDTO todo) { Todo persisted = Todo.getBuilder() .title(todo.getTitle()) .description(todo.getDescription()) .build(); persisted = repository.save(persisted); return convertToDTO(persisted); } @Override public TodoDTO delete(String id) { Todo deleted = findTodoById(id); repository.delete(deleted); return convertToDTO(deleted); } @Override public List findAll() { List todoEntries = repository.findAll(); return convertToDTOs(todoEntries); } private List convertToDTOs(List models) { return models.stream() .map(this::convertToDTO) .collect(toList()); } @Override public TodoDTO findById(String id) { Todo found = findTodoById(id); return convertToDTO(found); } @Override public TodoDTO update(TodoDTO todo) { Todo updated = findTodoById(todo.getId()); updated.update(todo.getTitle(), todo.getDescription()); updated = repository.save(updated); return convertToDTO(updated); } private Todo findTodoById(String id) { Optional result = repository.findOne(id); return result.orElseThrow(() -> new TodoNotFoundException(id)); } private TodoDTO convertToDTO(Todo model) { TodoDTO dto = new TodoDTO(); dto.setId(model.getId()); dto.setTitle(model.getTitle()); dto.setDescription(model.getDescription()); return dto; }} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| import org.hibernate.validator.constraints.NotEmpty;import javax.validation.constraints.Size;public final class TodoDTO { private String id; @Size(max = Todo.MAX_LENGTH_DESCRIPTION) private String description; @NotEmpty @Size(max = Todo.MAX_LENGTH_TITLE) private String title; //Constructor, getters, and setters are omitted} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;import java.util.List;@RestController@RequestMapping("/api/todo")final class TodoController { private final TodoService service; @Autowired TodoController(TodoService service) { this.service = service; } @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) TodoDTO create(@RequestBody @Valid TodoDTO todoEntry) { return service.create(todoEntry); } @RequestMapping(value = "{id}", method = RequestMethod.DELETE) TodoDTO delete(@PathVariable("id") String id) { return service.delete(id); } @RequestMapping(method = RequestMethod.GET) List return service.findAll(); } @RequestMapping(value = "{id}", method = RequestMethod.GET) TodoDTO findById(@PathVariable("id") String id) { return service.findById(id); } @RequestMapping(value = "{id}", method = RequestMethod.PUT) TodoDTO update(@RequestBody @Valid TodoDTO todoEntry) { return service.update(todoEntry); } @ExceptionHandler @ResponseStatus(HttpStatus.NOT_FOUND) public void handleTodoNotFound(TodoNotFoundException ex) { }} |

Comments
Post a Comment