001/* 002 * Copyright 2002-2017 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.support; 018 019import javax.xml.transform.Source; 020 021import org.springframework.http.converter.FormHttpMessageConverter; 022import org.springframework.http.converter.json.GsonHttpMessageConverter; 023import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 024import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter; 025import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; 026import org.springframework.http.converter.xml.SourceHttpMessageConverter; 027import org.springframework.util.ClassUtils; 028 029/** 030 * Extension of {@link org.springframework.http.converter.FormHttpMessageConverter}, 031 * adding support for XML and JSON-based parts. 032 * 033 * @author Rossen Stoyanchev 034 * @author Juergen Hoeller 035 * @since 3.2 036 */ 037public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConverter { 038 039 private static final boolean jaxb2Present = 040 ClassUtils.isPresent("javax.xml.bind.Binder", 041 AllEncompassingFormHttpMessageConverter.class.getClassLoader()); 042 043 private static final boolean jackson2Present = 044 ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", 045 AllEncompassingFormHttpMessageConverter.class.getClassLoader()) && 046 ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", 047 AllEncompassingFormHttpMessageConverter.class.getClassLoader()); 048 049 private static final boolean jackson2XmlPresent = 050 ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", 051 AllEncompassingFormHttpMessageConverter.class.getClassLoader()); 052 053 private static final boolean gsonPresent = 054 ClassUtils.isPresent("com.google.gson.Gson", 055 AllEncompassingFormHttpMessageConverter.class.getClassLoader()); 056 057 058 public AllEncompassingFormHttpMessageConverter() { 059 addPartConverter(new SourceHttpMessageConverter<Source>()); 060 061 if (jaxb2Present && !jackson2XmlPresent) { 062 addPartConverter(new Jaxb2RootElementHttpMessageConverter()); 063 } 064 065 if (jackson2Present) { 066 addPartConverter(new MappingJackson2HttpMessageConverter()); 067 } 068 else if (gsonPresent) { 069 addPartConverter(new GsonHttpMessageConverter()); 070 } 071 072 if (jackson2XmlPresent) { 073 addPartConverter(new MappingJackson2XmlHttpMessageConverter()); 074 } 075 } 076 077}