Skip to content

Instantly share code, notes, and snippets.

@SrikanthRao
Created May 4, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SrikanthRao/c9fc35e6fe22a74ab40c to your computer and use it in GitHub Desktop.
Save SrikanthRao/c9fc35e6fe22a74ab40c to your computer and use it in GitHub Desktop.
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();
}
}
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();
}
}
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();
}
}
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);
}
}
package com.fun.myapp;
import io.dropwizard.Configuration;
public class MyAppConfig extends Configuration {
public MyAppConfig() {
// TODO Auto-generated constructor stub
}
}
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