001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.http.converter.cbor; 018 019import com.fasterxml.jackson.databind.ObjectMapper; 020import com.fasterxml.jackson.dataformat.cbor.CBORFactory; 021 022import org.springframework.http.MediaType; 023import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; 024import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; 025import org.springframework.util.Assert; 026 027/** 028 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter 029 * HttpMessageConverter} that can read and write the <a href="https://cbor.io/">CBOR</a> 030 * data format using <a href="https://github.com/FasterXML/jackson-dataformats-binary/tree/master/cbor"> 031 * the dedicated Jackson 2.x extension</a>. 032 * 033 * <p>By default, this converter supports the {@link MediaType#APPLICATION_CBOR_VALUE} 034 * media type. This can be overridden by setting the {@link #setSupportedMediaTypes 035 * supportedMediaTypes} property. 036 * 037 * <p>The default constructor uses the default configuration provided by 038 * {@link Jackson2ObjectMapperBuilder}. 039 * 040 * <p>Compatible with Jackson 2.9 and higher. 041 * 042 * @author Sebastien Deleuze 043 * @since 5.0 044 */ 045public class MappingJackson2CborHttpMessageConverter extends AbstractJackson2HttpMessageConverter { 046 047 /** 048 * Construct a new {@code MappingJackson2CborHttpMessageConverter} using the 049 * default configuration provided by {@code Jackson2ObjectMapperBuilder}. 050 */ 051 public MappingJackson2CborHttpMessageConverter() { 052 this(Jackson2ObjectMapperBuilder.cbor().build()); 053 } 054 055 /** 056 * Construct a new {@code MappingJackson2CborHttpMessageConverter} with a 057 * custom {@link ObjectMapper} (must be configured with a {@code CBORFactory} 058 * instance). 059 * <p>You can use {@link Jackson2ObjectMapperBuilder} to build it easily. 060 * @see Jackson2ObjectMapperBuilder#cbor() 061 */ 062 public MappingJackson2CborHttpMessageConverter(ObjectMapper objectMapper) { 063 super(objectMapper, MediaType.APPLICATION_CBOR); 064 Assert.isInstanceOf(CBORFactory.class, objectMapper.getFactory(), "CBORFactory required"); 065 } 066 067 068 /** 069 * {@inheritDoc} 070 * The {@code ObjectMapper} must be configured with a {@code CBORFactory} instance. 071 */ 072 @Override 073 public void setObjectMapper(ObjectMapper objectMapper) { 074 Assert.isInstanceOf(CBORFactory.class, objectMapper.getFactory(), "CBORFactory required"); 075 super.setObjectMapper(objectMapper); 076 } 077 078}