Skip to content

Commit 94671b8

Browse files
committed
IllegalArgumentExceptionHandler, authorController, added readme page
1 parent 944332d commit 94671b8

14 files changed

+238
-85
lines changed

pom.xml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
<properties>
1717
<packaging>jar</packaging>
18-
<jdk.version>11</jdk.version>
19-
<release.version>11</release.version>
18+
<jdk.version>18</jdk.version>
19+
<release.version>18</release.version>
2020
<micronaut.version>3.5.1</micronaut.version>
2121
<micronaut.runtime>netty</micronaut.runtime>
2222
<exec.mainClass>com.example.Application</exec.mainClass>
@@ -105,10 +105,6 @@
105105
<artifactId>gson</artifactId>
106106
<version>2.9.0</version>
107107
</dependency>
108-
109-
110-
111-
112108
<dependency>
113109
<groupId>junit</groupId>
114110
<artifactId>junit</artifactId>
@@ -126,7 +122,6 @@
126122
<artifactId>cucumber-java</artifactId>
127123
<version>7.4.1</version>
128124
</dependency>
129-
130125
<dependency>
131126
<groupId>io.cucumber</groupId>
132127
<artifactId>cucumber-junit</artifactId>
@@ -167,6 +162,8 @@
167162
<arg>-Amicronaut.processing.group=com.example</arg>
168163
<arg>-Amicronaut.processing.module=demo</arg>
169164
</compilerArgs>
165+
<source>18</source>
166+
<target>18</target>
170167
</configuration>
171168
</plugin>
172169
</plugins>

src/main/java/com/example/controller/GreetController.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.example.controller.api;
2+
3+
import com.example.model.Author;
4+
import com.example.service.AuthorService;
5+
import io.micronaut.core.version.annotation.Version;
6+
import io.micronaut.http.HttpStatus;
7+
import io.micronaut.http.annotation.Body;
8+
import io.micronaut.http.annotation.Controller;
9+
import io.micronaut.http.annotation.Delete;
10+
import io.micronaut.http.annotation.Get;
11+
import io.micronaut.http.annotation.PathVariable;
12+
import io.micronaut.http.annotation.Post;
13+
import io.micronaut.http.annotation.Put;
14+
import io.micronaut.http.annotation.Status;
15+
import jakarta.inject.Inject;
16+
17+
import javax.validation.constraints.NotNull;
18+
import java.util.List;
19+
20+
import static com.example.generall.URlConstants.AUTHORS;
21+
import static com.example.generall.URlConstants.ID;
22+
import static com.example.generall.URlConstants.VERSION_1_1;
23+
24+
@Controller(AUTHORS)
25+
public class AuthorController {
26+
27+
@Inject
28+
private AuthorService service;
29+
30+
@Version(VERSION_1_1)
31+
@Get
32+
public List<Author> getAll() {
33+
return service.getAll();
34+
}
35+
36+
@Get(ID)
37+
public Author get(Long id) {
38+
return service.get(id);
39+
}
40+
41+
@Status(HttpStatus.CREATED)
42+
@Version(VERSION_1_1)
43+
@Post
44+
public void create(@Body Author author) {
45+
service.create(author);
46+
}
47+
48+
@Status(HttpStatus.ACCEPTED)
49+
@Version(VERSION_1_1)
50+
@Put(ID)
51+
public void update(@PathVariable @NotNull Long id, @Body @NotNull Author author) {
52+
service.update(id, author);
53+
}
54+
55+
@Status(HttpStatus.NO_CONTENT)
56+
@Version(VERSION_1_1)
57+
@Delete(ID)
58+
public void delete(@PathVariable @NotNull Long id) {
59+
service.delete(id);
60+
}
61+
}

src/main/java/com/example/controller/BookController.java renamed to src/main/java/com/example/controller/api/BookController.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.controller;
1+
package com.example.controller.api;
22

33
import com.example.model.Book;
44
import com.example.service.BookService;
@@ -20,7 +20,12 @@
2020
import java.util.List;
2121
import java.util.Optional;
2222

23-
@Controller("/book")
23+
import static com.example.generall.URlConstants.BOOKS;
24+
import static com.example.generall.URlConstants.COUNT;
25+
import static com.example.generall.URlConstants.ID;
26+
import static com.example.generall.URlConstants.VERSION_1_1;
27+
28+
@Controller(BOOKS)
2429
public class BookController {
2530

2631
@Inject
@@ -36,32 +41,31 @@ public List<Book> find(@QueryValue @Size(max = 1024, min = 1) Optional<String> t
3641
books = service.findAll();
3742
}
3843
return books;
39-
4044
}
4145

42-
@Version("1.1")
43-
@Get("/count")
46+
@Version(VERSION_1_1)
47+
@Get(COUNT)
4448
public Long count() {
4549
return service.countBooks();
4650
}
4751

48-
@Version("1.1")
52+
@Version(VERSION_1_1)
4953
@Status(HttpStatus.CREATED)
5054
@Post
5155
public void create(Book book) {
5256
service.create(book);
5357
}
5458

55-
@Version("1.1")
59+
@Version(VERSION_1_1)
5660
@Status(HttpStatus.NO_CONTENT)
57-
@Delete("{id}")
61+
@Delete(ID)
5862
public void delete(@PathVariable Long id) {
5963
service.delete(id);
6064
}
6165

62-
@Version("1.1")
66+
@Version(VERSION_1_1)
6367
@Status(HttpStatus.ACCEPTED)
64-
@Put("{id}")
68+
@Put(ID)
6569
public void update(@PathVariable Long id, @QueryValue @NotBlank String name) {
6670
service.updateName(id, name);
6771
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.example.controller.api;
2+
3+
import com.example.service.GreetingService;
4+
import io.micronaut.core.version.annotation.Version;
5+
import io.micronaut.http.HttpRequest;
6+
import io.micronaut.http.HttpResponse;
7+
import io.micronaut.http.MediaType;
8+
import io.micronaut.http.annotation.Controller;
9+
import io.micronaut.http.annotation.Get;
10+
import io.micronaut.http.annotation.Produces;
11+
import jakarta.inject.Inject;
12+
13+
@Controller
14+
public class GreetController {
15+
16+
@Inject
17+
private GreetingService greetingService;
18+
19+
@Version("1.0")
20+
@Get("/{name}")
21+
public String greet(String name) {
22+
return greetingService.getGreeting() + " " + name;
23+
}
24+
25+
@Version("1.1")
26+
@Produces(MediaType.TEXT_PLAIN)
27+
@Get(value = "/hello")
28+
public HttpResponse<String> hello(HttpRequest<?> request) {
29+
30+
String name = request.getParameters()
31+
.getFirst("name")
32+
.orElse("Nobody");
33+
34+
return HttpResponse.ok("Hello " + name + "!!")
35+
.header("X-My-Header", "Hi in headers");
36+
}
37+
38+
@Produces(MediaType.TEXT_HTML)
39+
@Get
40+
public String readMe() {
41+
return """
42+
<!DOCTYPE html>
43+
<html>
44+
<head>
45+
<title>README.md</title>
46+
</head>
47+
<body>
48+
<h1>Micronaut 3.5.1 Documentation </h1>
49+
<a href="https://docs.micronaut.io/3.5.1/guide/index.html">User Guide</a>
50+
<a href="https://docs.micronaut.io/3.5.1/api/index.html">API Reference</a>
51+
<a href="https://docs.micronaut.io/3.5.1/guide/configurationreference.html">Configuration Reference</a>
52+
<a href="https://guides.micronaut.io/index.html">Micronaut Guides</a>
53+
<h1>Feature http-client documentation</h1>
54+
<a href="https://docs.micronaut.io/latest/guide/index.html#httpClient">Micronaut HTTP Client documentation</a>
55+
</body>
56+
</html>
57+
""";
58+
}
59+
}

src/main/java/com/example/controller/api/URlConstants.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.exceptionHandler;
2+
3+
import io.micronaut.context.annotation.Requires;
4+
import io.micronaut.http.HttpRequest;
5+
import io.micronaut.http.HttpResponse;
6+
import io.micronaut.http.annotation.Produces;
7+
import io.micronaut.http.server.exceptions.ExceptionHandler;
8+
import io.micronaut.http.server.exceptions.response.ErrorContext;
9+
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor;
10+
import jakarta.inject.Singleton;
11+
12+
@Produces
13+
@Singleton
14+
@Requires(classes = {IllegalArgumentException.class, ExceptionHandler.class})
15+
public class IllegalArgumentExceptionHandler implements ExceptionHandler<IllegalArgumentException, HttpResponse> {
16+
17+
private final ErrorResponseProcessor<?> errorResponseProcessor;
18+
19+
public IllegalArgumentExceptionHandler(ErrorResponseProcessor<?> errorResponseProcessor) {
20+
this.errorResponseProcessor = errorResponseProcessor;
21+
}
22+
23+
@Override
24+
public HttpResponse handle(HttpRequest request, IllegalArgumentException exception) {
25+
return errorResponseProcessor.processResponse
26+
(ErrorContext.builder(request)
27+
.cause(exception)
28+
.errorMessage(exception.getMessage())
29+
.build(), HttpResponse.badRequest());
30+
}
31+
}

src/main/java/com/example/exceptionHandler/ItemNotFoundExceptionHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ public HttpResponse handle(HttpRequest request, ItemNotFoundException e) {
3030
.errorMessage(e.getMessage())
3131
.build(), HttpResponse.badRequest());
3232
}
33-
3433
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.example.exceptions;
22

3+
import static com.example.generall.Constraints.ITEM_NOT_FOUND;
4+
35
public class ItemNotFoundException extends RuntimeException {
46

57
public ItemNotFoundException(String msg) {
68
super(msg);
79
}
810

911
public ItemNotFoundException() {
10-
super("Item not found");
12+
super(ITEM_NOT_FOUND);
1113
}
1214
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.generall;
2+
3+
public class Constraints {
4+
5+
public static final String ITEM_NOT_FOUND = "Item not found";
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.generall;
2+
3+
public final class URlConstants {
4+
5+
public URlConstants() throws IllegalAccessException {
6+
throw new IllegalAccessException("it is just for constants");
7+
}
8+
9+
public static final String BOOKS = "books";
10+
public static final String COUNT = "count";
11+
12+
public static final String AUTHORS = "authors";
13+
public static final String VERSION_1_1 = "1.1";
14+
public static final String ID = "{id}";
15+
}

src/main/java/com/example/repository/AuthorRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.example.model.Author;
44
import io.micronaut.data.annotation.Repository;
5-
import io.micronaut.data.repository.CrudRepository;
5+
import io.micronaut.data.jpa.repository.JpaRepository;
66

77
@Repository
8-
public interface AuthorRepository extends CrudRepository<Author, Long> {
8+
public interface AuthorRepository extends JpaRepository<Author, Long> {
99
}

0 commit comments

Comments
 (0)