Created
May 4, 2015 18:39
-
-
Save SrikanthRao/c9fc35e6fe22a74ab40c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import javax.ws.rs.BeanParam; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.QueryParam; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
@Path("/date") | |
@Produces(MediaType.APPLICATION_JSON) | |
@Consumes(MediaType.APPLICATION_JSON) | |
public class DateResource { | |
public DateResource() { | |
// empty constructor | |
} | |
@GET | |
public Response isValidDate(@QueryParam("date") LocalDateTimeParam date) { | |
return Response.ok().build(); | |
} | |
@GET | |
@Path("/bean") | |
public Response isValidDate(@BeanParam PaginationFilters filter) { | |
return Response.ok().build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import io.dropwizard.jersey.params.AbstractParam; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.LocalTime; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.core.Response.Status; | |
/** | |
* Converts ISO_LOCAL_DATE to {@code LocalDateTime} | |
* | |
*/ | |
public class LocalDateTimeParam extends AbstractParam<LocalDateTime> { | |
public LocalDateTimeParam(String input) { | |
super(input); | |
} | |
@Override | |
protected LocalDateTime parse(String input) throws Exception { | |
return LocalDateTime.of(LocalDate.parse(input), LocalTime.MIDNIGHT); | |
} | |
@Override | |
protected Response error(String input, Exception e) { | |
return Response | |
.status(Status.BAD_REQUEST) | |
.type(MediaType.APPLICATION_JSON_TYPE) | |
.entity("Date is either not in YYYY-MM-DD format or is invalid") | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import org.glassfish.hk2.api.MultiException; | |
import javax.ws.rs.WebApplicationException; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.ExceptionMapper; | |
public class MultiExceptionMapper implements ExceptionMapper<MultiException> { | |
@Override | |
public Response toResponse(MultiException exception) { | |
for (Throwable ex : exception.getErrors()) { | |
if (WebApplicationException.class.isAssignableFrom(ex.getClass())) { | |
return ((WebApplicationException) ex).getResponse(); | |
} | |
} | |
return Response | |
.serverError() | |
.entity("Internal Error occurred.") | |
.type(MediaType.APPLICATION_JSON_TYPE) | |
.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import io.dropwizard.Application; | |
import io.dropwizard.setup.Environment; | |
public class MyApp extends Application<MyAppConfig> { | |
@Override | |
public void run(MyAppConfig configuration, Environment environment) throws Exception { | |
environment.jersey().register(DateResource.class); | |
// adding the following exception mapper handles MultiException well. i.e. produces error | |
// indicated by *Param class | |
// environment.jersey().register(MultiExceptionMapper.class); | |
} | |
public static void main(String[] args) throws Exception { | |
new MyApp().run(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import io.dropwizard.Configuration; | |
public class MyAppConfig extends Configuration { | |
public MyAppConfig() { | |
// TODO Auto-generated constructor stub | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fun.myapp; | |
import java.time.LocalDateTime; | |
import java.util.Optional; | |
import javax.validation.constraints.NotNull; | |
import javax.ws.rs.QueryParam; | |
public class PaginationFilters { | |
public PaginationFilters() { | |
// empty constructor | |
} | |
@NotNull(message = "date must be specified") | |
@QueryParam("date") | |
private LocalDateTimeParam date; | |
public Optional<LocalDateTime> getDate() { | |
if (date != null) { | |
return Optional.of(date.get()); | |
} | |
else { | |
return Optional.empty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment