Skip to content

Commit

Permalink
demo-jetty: custom converter example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed Dec 28, 2020
1 parent 502b102 commit f852912
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="MethodLength" />
<module name="ParameterNumber">
<!-- module name="ParameterNumber">
<property name="max" value="10" />
</module>
</module -->

<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import net.cactusthorn.routing.demo.jetty.dagger.*;

import java.time.LocalDate;

import javax.servlet.MultipartConfigElement;

import org.eclipse.jetty.server.*;
Expand All @@ -32,6 +34,7 @@ public static void main(String... args) {
.addProducer("application/json", new SimpleGsonProducer())
.addConsumer("application/json", new SimpleGsonConsumer())
.addProducer("text/html", new SimpleThymeleafProducer("/thymeleaf/"))
.addConverter(LocalDate.class, new LocalDateConverter())
.build();
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.cactusthorn.routing.demo.jetty;

import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.convert.Converter;
import net.cactusthorn.routing.convert.ConverterException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class LocalDateConverter implements Converter {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("ddMMyyyy");

@Override //
public LocalDate convert(RequestData requestData, Class<?> type, String value) throws ConverterException {
if (value == null || value.trim().isEmpty()) {
return null;
}
try {
return LocalDate.parse(value, FORMATTER);
} catch (DateTimeParseException e) {
throw new ConverterException("LocalDate converting failed", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDate;

import javax.inject.Inject;

Expand Down Expand Up @@ -42,4 +43,9 @@ public String empty(@PathParam("var") @DefaultValue("DEFAULT") String sss) {
public Response seeother() throws URISyntaxException {
return Response.builder().seeOther(new URI("/rest/api/test30?test=33.45")).build();
}

@GET @Path("/localdate/{date : \\d{8}}") //
public String localdate(@PathParam("date") LocalDate date) throws URISyntaxException {
return date.toString();
}
}

0 comments on commit f852912

Please sign in to comment.