Commit 37e99ce0 authored by Matija Obreza's avatar Matija Obreza

Cache serialized JSON of /api/v1/crops

- Serialization of objects takes a long time
parent 41017532
......@@ -30,7 +30,10 @@ import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.CropService.CropDetails;
import org.genesys2.server.service.impl.CropServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -42,9 +45,17 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module.Feature;
import io.swagger.annotations.Api;
import net.sf.oval.ConstraintViolation;
......@@ -62,16 +73,45 @@ public class CropsController extends ApiBaseController {
@Autowired
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
private static ObjectMapper objectMapper;
static {
objectMapper = new ObjectMapper();
Hibernate4Module hibernateModule = new Hibernate4Module();
hibernateModule.disable(Feature.FORCE_LAZY_LOADING);
hibernateModule.disable(Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
objectMapper.registerModule(hibernateModule);
// serialization
objectMapper.disable(SerializationFeature.EAGER_SERIALIZER_FETCH);
// deserialization
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
// // Never ignore stuff we don't understand
// mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// ignore stuff we don't understand
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// explicit json views: every fields needs to be annotated, therefore enabled
objectMapper.enable(MapperFeature.DEFAULT_VIEW_INCLUSION);
// enable upgrading to arrays
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}
/**
* List of Crop details
*
* @return list of crop details
* @throws JsonProcessingException
*/
@RequestMapping(value = "", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CropService.CropDetails> listCropDetails() {
@Cacheable(value = CropServiceImpl.CACHE_CROPS, key = "'api-v1-cropdetails'", unless = "#result == null")
public @ResponseBody String listCropDetails() throws JsonProcessingException {
LOG.info("Listing crop details");
return cropService.listDetails(LocaleContextHolder.getLocale());
List<CropDetails> crops = cropService.listDetails(LocaleContextHolder.getLocale());
LOG.debug("Got crops");
String json = objectMapper.writeValueAsString(crops);
LOG.debug("Serialized to JSON");
return json;
}
/**
......
......@@ -96,7 +96,7 @@ import com.querydsl.core.BooleanBuilder;
public class CropServiceImpl implements CropService {
private static final String CACHE_CROP_TAXONOMYCROPS = "hibernate.org.genesys2.server.model.impl.Crop.taxonomyCrops";
private static final String CACHE_CROPS = "hibernate.org.genesys2.server.model.impl.Crop.cache";
public static final String CACHE_CROPS = "hibernate.org.genesys2.server.model.impl.Crop.cache";
public static final Logger LOG = LoggerFactory.getLogger(CropServiceImpl.class);
......
......@@ -16,6 +16,7 @@
package org.genesys2.spring.config;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;
......@@ -37,6 +38,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
......@@ -61,6 +63,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module.Feature;
import com.google.common.base.Charsets;
/**
* Spring MVC
......@@ -258,16 +261,22 @@ public class WebConfiguration implements WebMvcConfigurer {
return mapper;
}
public StringHttpMessageConverter stringConverter() {
final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charsets.UTF_8);
stringConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML, MediaType.APPLICATION_JSON));
return stringConverter;
}
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
// String-type returns get written directly
converters.add(stringConverter());
// Here we add our custom-configured HttpMessageConverter
converters.add(jacksonMessageConverter());
// Custom CSV converter
converters.add(new CSVMessageConverter<Object>(objectMapper()));
// Raw request body -- needed for uploading raw files
converters.add(new ByteArrayHttpMessageConverter());
// HTTP string
converters.add(new StringHttpMessageConverter());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment