org.springframework.http.converter.json.GsonHttpMessageConverter Java Examples

The following examples show how to use org.springframework.http.converter.json.GsonHttpMessageConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccountControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@BeforeEach
void setUp() {
  FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
  freeMarkerViewResolver.setSuffix(".ftl");
  mockMvc =
      MockMvcBuilders.standaloneSetup(authenticationController)
          .setMessageConverters(
              new FormHttpMessageConverter(), new GsonHttpMessageConverter(new Gson()))
          .build();

  reset(authenticationSettings);
  reset(reCaptchaService);
  reset(appSettings);
  when(reCaptchaService.validate("validCaptcha")).thenReturn(true);
  reset(accountService); // mocks in the config class are not resetted after each test
}
 
Example #2
Source File: AppManagerControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@BeforeEach
void beforeMethod() {
  initMocks(this);

  App app = mock(App.class);
  when(app.getId()).thenReturn("id");
  when(app.getName()).thenReturn("app1");
  when(app.getLabel()).thenReturn("label");
  when(app.getDescription()).thenReturn("description");
  when(app.isActive()).thenReturn(true);
  when(app.getAppVersion()).thenReturn("v1.0.0");
  when(app.includeMenuAndFooter()).thenReturn(true);
  when(app.getTemplateContent()).thenReturn("<h1>Test</h1>");
  when(app.getAppConfig()).thenReturn("{'config': 'test'}");
  when(app.getResourceFolder()).thenReturn("resource-folder");
  appResponse = AppResponse.create(app);

  AppManagerController controller = new AppManagerController(appManagerService, dataService);
  mockMvc =
      MockMvcBuilders.standaloneSetup(controller)
          .setMessageConverters(new GsonHttpMessageConverter(new Gson()))
          .build();
}
 
Example #3
Source File: AllEncompassingFormHttpMessageConverter.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public AllEncompassingFormHttpMessageConverter() {
	addPartConverter(new SourceHttpMessageConverter<>());

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}
	/*else if (jsonbPresent) {
		addPartConverter(new JsonbHttpMessageConverter());
	}*/

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}

	/*if (jackson2SmilePresent) {
		addPartConverter(new MappingJackson2SmileHttpMessageConverter());
	}*/
}
 
Example #4
Source File: AllEncompassingFormHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public AllEncompassingFormHttpMessageConverter() {
	addPartConverter(new SourceHttpMessageConverter<Source>());

	if (jaxb2Present && !jackson2Present) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}
}
 
Example #5
Source File: AllEncompassingFormHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public AllEncompassingFormHttpMessageConverter() {
	addPartConverter(new SourceHttpMessageConverter<Source>());

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}
}
 
Example #6
Source File: AllEncompassingFormHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public AllEncompassingFormHttpMessageConverter() {
	try {
		addPartConverter(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		addPartConverter(new JsonbHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		addPartConverter(new MappingJackson2SmileHttpMessageConverter());
	}
}
 
Example #7
Source File: RestController.java    From telekom-workflow-engine with MIT License 5 votes vote down vote up
@PostConstruct
public void init(){
    for (HttpMessageConverter converter : adapter.getMessageConverters()) {
        if (converter instanceof GsonHttpMessageConverter) {
            Gson gson = new GsonBuilder().serializeNulls().create();
            ((GsonHttpMessageConverter)converter).setGson(gson);
        }
    }
}
 
Example #8
Source File: PresentationConfiguration.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
public static void configureMessageConverters(List<HttpMessageConverter<?>> converters, Gson gson) {
    // Rend possible la production de texte (getFile)
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
    stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
    converters.add(stringHttpMessageConverter);
    // Rend possible l'utilisation de Gson pour la sérialisation/désérialisation
    GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
    gsonHttpMessageConverter.setGson(gson);
    converters.add(gsonHttpMessageConverter);
}
 
Example #9
Source File: SpringWebMvcConfigurer.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Use GSON instead of Jackson for JSON serialization
 * @param converters
 */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters)
{
	// Could not figure out a better way to force GSON
	converters.removeIf(MappingJackson2HttpMessageConverter.class::isInstance);

	GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
	gsonHttpMessageConverter.setGson(RuneLiteAPI.GSON);
	converters.add(gsonHttpMessageConverter);
}
 
Example #10
Source File: WebConfig.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new ResourceHttpMessageConverter());

    GsonHttpMessageConverter converter = new GsonHttpMessageConverter();

    converter.setGson(gson());
    converters.add(converter);
}
 
Example #11
Source File: RestTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter());
	this.messageConverters.add(new SourceHttpMessageConverter<Source>());
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example #12
Source File: HttpMessageConverterConfiguration.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Bean
public HttpMessageConverters messageConverters() {
  GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
  gsonHttpMessageConverter.setGson(
          new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create());
  final List<HttpMessageConverter<?>> converters = Lists.newArrayList(
          new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(),
          new AllEncompassingFormHttpMessageConverter(), gsonHttpMessageConverter);
  return new HttpMessageConverters() {
    @Override
    public List<HttpMessageConverter<?>> getConverters() {
      return converters;
    }
  };
}
 
Example #13
Source File: RestTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter());
	this.messageConverters.add(new SourceHttpMessageConverter<Source>());
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example #14
Source File: TraderMainConfiguration.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new StringHttpMessageConverter());
    converters.add(new FormHttpMessageConverter());
    GsonHttpMessageConverter c = new GsonHttpMessageConverter();
    c.setGson(new GsonBuilder().disableHtmlEscaping().create());
    converters.add(c);
    converters.add(new ResourceHttpMessageConverter());
}
 
Example #15
Source File: TraderUMainConfiguration.java    From java-trader with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new StringHttpMessageConverter());
    converters.add(new FormHttpMessageConverter());
    GsonHttpMessageConverter c = new GsonHttpMessageConverter();
    c.setGson(new GsonBuilder().disableHtmlEscaping().create());
    converters.add(c);
    converters.add(new ResourceHttpMessageConverter());
}
 
Example #16
Source File: AllEncompassingFormHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
public AllEncompassingFormHttpMessageConverter() {
	try {
		addPartConverter(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}

	if (jaxb2Present && !jackson2XmlPresent) {
		addPartConverter(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		addPartConverter(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		addPartConverter(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		addPartConverter(new JsonbHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		addPartConverter(new MappingJackson2XmlHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		addPartConverter(new MappingJackson2SmileHttpMessageConverter());
	}
}
 
Example #17
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a set of default HttpMessageConverter instances to the given list.
 * Subclasses can call this method from {@link #configureMessageConverters(List)}.
 * @param messageConverters the list to add the default message converters to
 */
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
	StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
	stringConverter.setWriteAcceptCharset(false);

	messageConverters.add(new ByteArrayHttpMessageConverter());
	messageConverters.add(stringConverter);
	messageConverters.add(new ResourceHttpMessageConverter());
	messageConverters.add(new SourceHttpMessageConverter<Source>());
	messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		messageConverters.add(new AtomFeedHttpMessageConverter());
		messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		messageConverters.add(new MappingJackson2XmlHttpMessageConverter(
				Jackson2ObjectMapperBuilder.xml().applicationContext(this.applicationContext).build()));
	}
	else if (jaxb2Present) {
		messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		messageConverters.add(new MappingJackson2HttpMessageConverter(
				Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build()));
	}
	else if (gsonPresent) {
		messageConverters.add(new GsonHttpMessageConverter());
	}
}
 
Example #18
Source File: RestTemplate.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter HttpMessageConverters} are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter(false));
	try {
		this.messageConverters.add(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		this.messageConverters.add(new JsonbHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter());
	}
	if (jackson2CborPresent) {
		this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
	}

	this.uriTemplateHandler = initUriTemplateHandler();
}
 
Example #19
Source File: RestTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter HttpMessageConverters} are initialized.
 */
public RestTemplate() {
	this.messageConverters.add(new ByteArrayHttpMessageConverter());
	this.messageConverters.add(new StringHttpMessageConverter());
	this.messageConverters.add(new ResourceHttpMessageConverter(false));
	try {
		this.messageConverters.add(new SourceHttpMessageConverter<>());
	}
	catch (Error err) {
		// Ignore when no TransformerFactory implementation is available
	}
	this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

	if (romePresent) {
		this.messageConverters.add(new AtomFeedHttpMessageConverter());
		this.messageConverters.add(new RssChannelHttpMessageConverter());
	}

	if (jackson2XmlPresent) {
		this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
	}
	else if (jaxb2Present) {
		this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
	}

	if (jackson2Present) {
		this.messageConverters.add(new MappingJackson2HttpMessageConverter());
	}
	else if (gsonPresent) {
		this.messageConverters.add(new GsonHttpMessageConverter());
	}
	else if (jsonbPresent) {
		this.messageConverters.add(new JsonbHttpMessageConverter());
	}

	if (jackson2SmilePresent) {
		this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter());
	}
	if (jackson2CborPresent) {
		this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
	}

	this.uriTemplateHandler = initUriTemplateHandler();
}
 
Example #20
Source File: GsonConfig.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter() {
  return new GsonHttpMessageConverter(gsonFactoryBean().getObject());
}
 
Example #21
Source File: GsonConfig.java    From spring-boot-study with MIT License 3 votes vote down vote up
@Bean
public HttpMessageConverters customConverters() {

    Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();

    GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
    messageConverters.add(gsonHttpMessageConverter);

    return new HttpMessageConverters(true, messageConverters);
}