Last active
December 4, 2020 05:02
-
-
Save psamsotha/429116f0049cea2423fc4580112708f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Locale; | |
import java.util.Map; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.Provider; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import com.fasterxml.jackson.annotation.JsonInclude; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; | |
/** | |
* <dependency> | |
* <groupId>org.glassfish.jersey.media</groupId> | |
* <artifactId>jersey-media-json-jackson</artifactId> | |
* </dependency> | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.inject</groupId> | |
* <artifactId>jersey-hk2</artifactId> | |
* </dependency> | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* </dependency> | |
*/ | |
public class JacksonDateTest extends JerseyTest { | |
static class DateUtils { | |
static String ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; | |
} | |
@Provider | |
@Consumes(MediaType.APPLICATION_JSON) | |
@Produces(MediaType.APPLICATION_JSON) | |
public static class JacksonProvider extends JacksonJaxbJsonProvider { | |
public static final ObjectMapper mapper; | |
static { | |
mapper = new ObjectMapper() | |
// DateUtils.ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" | |
.setDateFormat(new SimpleDateFormat(DateUtils.ISO_DATE_FORMAT, Locale.getDefault())) | |
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | |
.setSerializationInclusion(JsonInclude.Include.NON_NULL); | |
} | |
public JacksonProvider() { | |
super(); | |
setMapper(mapper); | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new Application(); | |
} | |
public class Application extends ResourceConfig { | |
public Application() { | |
register(JacksonProvider.class); | |
register(ExampleEndpoint.class); | |
} | |
} | |
@Path("/example") | |
public static class ExampleEndpoint { | |
@GET | |
@Produces(MediaType.APPLICATION_JSON) | |
public Response getTimestamp() { | |
try { | |
Map<String, Object> response = new HashMap<>(); | |
response.put("current_timestamp", new Date()); | |
return Response.ok(response).build(); | |
} catch (Exception e) { | |
return Response.serverError().entity(e).build(); | |
} | |
} | |
} | |
@Test | |
public void testIt() { | |
String res = target("example").request().get(String.class); | |
System.out.println(res); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment