Skip to content

Commit

Permalink
add the Jackson ObjectMapper customizations provided by spring-boot (#11
Browse files Browse the repository at this point in the history
)
  • Loading branch information
scizeron authored and gregwhitaker committed Sep 7, 2017
1 parent 8ad6119 commit c79baf0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 48 deletions.
14 changes: 14 additions & 0 deletions catnap-examples/catnap-examples-springboot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,17 @@ This example will only retrieve the name of the widget and the urls of images th
}
]
```

## JSON representation
You can customize the JSON rendering using the the Jackson ObjectMapper customizations provided by spring-boot.

See <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper">"Customize the Jackson ObjectMapper"</a> section in Spring Boot documentation.

* Display dates as timestamps

![write_dates_as_timestamps](docs/images/write_dates_as_timestamps_true.png)

* Display dates as strings (UTC)

![write_dates_as_UTC_strings](docs/images/write_dates_as_timestamps_false.png)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@

package catnap.examples.springboot;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.gregwhitaker.catnap.springboot.annotation.EnableCatnap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.github.gregwhitaker.catnap.springboot.annotation.EnableCatnap;

@SpringBootApplication
@EnableCatnap
Expand All @@ -32,18 +28,4 @@ public class WidgetApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(WidgetApplication.class, args);
}

/*
* This is how you define a custom object mapper to be used with Catnap. If you
* do not need a custom object mapper you do not need to define this method as Catnap
* will create a default object mapper for you.
*/
@Bean
public ObjectMapper myCustomObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
mapper.setDateFormat(df);

return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring:
jackson:
serialization:
write_dates_as_timestamps: false
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@

package com.github.gregwhitaker.catnap.springboot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.gregwhitaker.catnap.core.view.JsonCatnapView;
import com.github.gregwhitaker.catnap.core.view.JsonpCatnapView;
import com.github.gregwhitaker.catnap.springboot.messageconverters.CatnapJsonMessageConverter;
import com.github.gregwhitaker.catnap.springboot.messageconverters.CatnapJsonpMessageConverter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.gregwhitaker.catnap.core.view.JsonCatnapView;
import com.github.gregwhitaker.catnap.core.view.JsonpCatnapView;
import com.github.gregwhitaker.catnap.springboot.messageconverters.CatnapJsonMessageConverter;
import com.github.gregwhitaker.catnap.springboot.messageconverters.CatnapJsonpMessageConverter;

@Configuration
public class CatnapWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

@Autowired(required = false)
private ObjectMapper mapper;
/**
* @see See "Customize the Jackson ObjectMapper" section in Spring Boot documentation
*/
@Autowired
private Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder;

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
Expand All @@ -45,25 +49,10 @@ public void configureContentNegotiation(ContentNegotiationConfigurer configurer)

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(catnapJsonMessageConverter());
converters.add(catnapJsonpMessageConverter());
}

public CatnapJsonMessageConverter catnapJsonMessageConverter() {
if (mapper != null) {
return new CatnapJsonMessageConverter(new JsonCatnapView.Builder()
.withObjectMapper(mapper).build());
} else {
return new CatnapJsonMessageConverter();
}
}

public CatnapJsonpMessageConverter catnapJsonpMessageConverter() {
if (mapper != null) {
return new CatnapJsonpMessageConverter(new JsonpCatnapView.Builder()
.withObjectMapper(mapper).build());
} else {
return new CatnapJsonpMessageConverter();
}
final ObjectMapper objectMapper = this.jackson2ObjectMapperBuilder.build();
converters.add(new CatnapJsonMessageConverter(new JsonCatnapView.Builder()
.withObjectMapper(objectMapper).build()));
converters.add(new CatnapJsonpMessageConverter(new JsonpCatnapView.Builder()
.withObjectMapper(objectMapper).build()));
}
}

0 comments on commit c79baf0

Please sign in to comment.