Java Code Examples for com.fasterxml.jackson.dataformat.xml.XmlMapper#configure()

The following examples show how to use com.fasterxml.jackson.dataformat.xml.XmlMapper#configure() . 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: ApiClient.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Build the RestTemplate used to make HTTP requests.
 * @return RestTemplate
 */
protected RestTemplate buildRestTemplate() {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
    xmlMapper.registerModule(new JsonNullableModule());
    messageConverters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper));

    RestTemplate restTemplate = new RestTemplate(messageConverters);
    
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            ThreeTenModule module = new ThreeTenModule();
            module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
            module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
            module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
            mapper.registerModule(module);
            mapper.registerModule(new JsonNullableModule());
        }
    }
    // This allows us to read the response more than once - Necessary for debugging.
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
    return restTemplate;
}
 
Example 2
Source File: WxUtils.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
public static <T> T xmlToBean(String xml, Class<T> clazz) {
	 Assert.hasLength(xml,"xml 为空");
	 XmlMapper xmlMapper = new XmlMapper();
	 xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

	try {
		return xmlMapper.readValue(xml, clazz);
	} catch (IOException e) {
		throw new ServiceException(e.getMessage());
	}
}
 
Example 3
Source File: SysomosXmlSerDeIT.java    From streams with Apache License 2.0 5 votes vote down vote up
/**
 * before.
 */
@BeforeClass
public void before() {

  XmlFactory xmlFactory = new XmlFactory(new InputFactoryImpl(),
      new OutputFactoryImpl());

  JacksonXmlModule module = new JacksonXmlModule();

  module.setDefaultUseWrapper(false);

  xmlMapper = new XmlMapper(xmlFactory, module);

  xmlMapper
      .configure(
          DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
          Boolean.TRUE);
  xmlMapper
      .configure(
          DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
          Boolean.TRUE);
  xmlMapper
      .configure(
          DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY,
          Boolean.TRUE);
  xmlMapper.configure(
      DeserializationFeature.READ_ENUMS_USING_TO_STRING,
      Boolean.TRUE);

}
 
Example 4
Source File: XML.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static XmlMapper newMapper() {
	XmlMapper mapper = new XmlMapper();
	mapper.setBase64Variant(Base64Variants.MODIFIED_FOR_URL);
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

	if (!Env.dev()) {
		mapper.registerModule(new AfterburnerModule());
	}

	return mapper;
}
 
Example 5
Source File: JobConfigurationSerializationTest.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void xmlWithArray() throws IOException
{
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
    xmlMapper.configure( DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false );

    JobConfiguration jc = xmlMapper.readValue( "<jobConfiguration lastUpdated=\"2019-03-26T22:57:44.649\" id=\"uB9oC4R2nTn\" created=\"2019-03-26T22:57:44.649\" name=\"Test Analytic\">\n" +
        "      <jobStatus>NONE</jobStatus>\n" +
        "      <displayName>Test Analytic</displayName>\n" +
        "      <enabled>true</enabled>\n" +
        "      <leaderOnlyJob>true</leaderOnlyJob>\n" +
        "      <externalAccess>false</externalAccess>\n" +
        "      <jobType>ANALYTICS_TABLE</jobType>\n" +
        "      <nextExecutionTime>2019-03-27T12:00:00.000</nextExecutionTime>\n" +
        "      <favorite>false</favorite>\n" +
        "      <configurable>true</configurable>\n" +
        "      <access>\n" +
        "        <read>true</read>\n" +
        "        <update>true</update>\n" +
        "        <externalize>false</externalize>\n" +
        "        <delete>true</delete>\n" +
        "        <write>true</write>\n" +
        "        <manage>true</manage>\n" +
        "      </access>\n" +
        "      <lastUpdatedBy id=\"xE7jOejl9FI\"/>\n" +
        "      <jobParameters>\n" +
        "        <lastYears>2</lastYears>\n" +
        "        <skipResourceTables>true</skipResourceTables>\n" +
        "        <skipTableTypes>\n" +
        "          <skipTableType>ENROLLMENT</skipTableType>\n" +
        "          <skipTableType>ORG_UNIT_TARGET</skipTableType>\n" +
        "          <skipTableType>VALIDATION_RESULT</skipTableType>\n" +
        "        </skipTableTypes>" +
        "      </jobParameters>\n" +
        "      <cronExpression>0 0 12 ? * MON-FRI</cronExpression>\n" +
        "    </jobConfiguration>", JobConfiguration.class );

    Assert.assertEquals( JobStatus.SCHEDULED, jc.getJobStatus() );
    Assert.assertEquals( "Test Analytic", jc.getDisplayName() );
    Assert.assertTrue( jc.isEnabled() );
    Assert.assertTrue( jc.isLeaderOnlyJob() );
    Assert.assertEquals( JobType.ANALYTICS_TABLE, jc.getJobType() );
    Assert.assertNull( jc.getNextExecutionTime() );
    Assert.assertEquals( "0 0 12 ? * MON-FRI", jc.getCronExpression() );

    Assert.assertNotNull( jc.getJobParameters() );
    Assert.assertEquals( (Integer) 2, ( (AnalyticsJobParameters) jc.getJobParameters() ).getLastYears() );
    Assert.assertTrue( ( (AnalyticsJobParameters) jc.getJobParameters() ).isSkipResourceTables() );
    Assert.assertNotNull( ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes() );
    Assert.assertEquals( 3, ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes().size() );
    Assert.assertThat( ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes(), Matchers.hasItems( AnalyticsTableType.ENROLLMENT, AnalyticsTableType.ORG_UNIT_TARGET, AnalyticsTableType.VALIDATION_RESULT ) );
}
 
Example 6
Source File: JobConfigurationSerializationTest.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void xmlWithEmptyArray() throws IOException
{
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
    xmlMapper.configure( DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false );

    JobConfiguration jc = xmlMapper.readValue( "<jobConfiguration lastUpdated=\"2019-03-26T22:57:44.649\" id=\"uB9oC4R2nTn\" created=\"2019-03-26T22:57:44.649\" name=\"Test Analytic\">\n" +
        "      <jobStatus>SCHEDULED</jobStatus>\n" +
        "      <displayName>Test Analytic</displayName>\n" +
        "      <enabled>true</enabled>\n" +
        "      <leaderOnlyJob>true</leaderOnlyJob>\n" +
        "      <externalAccess>false</externalAccess>\n" +
        "      <jobType>ANALYTICS_TABLE</jobType>\n" +
        "      <nextExecutionTime>2019-03-27T12:00:00.000</nextExecutionTime>\n" +
        "      <favorite>false</favorite>\n" +
        "      <configurable>true</configurable>\n" +
        "      <access>\n" +
        "        <read>true</read>\n" +
        "        <update>true</update>\n" +
        "        <externalize>false</externalize>\n" +
        "        <delete>true</delete>\n" +
        "        <write>true</write>\n" +
        "        <manage>true</manage>\n" +
        "      </access>\n" +
        "      <lastUpdatedBy id=\"xE7jOejl9FI\"/>\n" +
        "      <jobParameters>\n" +
        "        <lastYears>2</lastYears>\n" +
        "        <skipResourceTables>true</skipResourceTables>\n" +
        "        <skipTableTypes>\n" +
        "        </skipTableTypes>" +
        "      </jobParameters>\n" +
        "      <cronExpression>0 0 12 ? * MON-FRI</cronExpression>\n" +
        "    </jobConfiguration>", JobConfiguration.class );

    Assert.assertEquals( "uB9oC4R2nTn", jc.getUid() );
    Assert.assertEquals( JobStatus.SCHEDULED, jc.getJobStatus() );
    Assert.assertEquals( "Test Analytic", jc.getName() );
    Assert.assertEquals( "Test Analytic", jc.getDisplayName() );
    Assert.assertTrue( jc.isEnabled() );
    Assert.assertTrue( jc.isLeaderOnlyJob() );
    Assert.assertEquals( JobType.ANALYTICS_TABLE, jc.getJobType() );
    Assert.assertNull( jc.getNextExecutionTime() );
    Assert.assertEquals( "0 0 12 ? * MON-FRI", jc.getCronExpression() );

    Assert.assertNotNull( jc.getJobParameters() );
    Assert.assertEquals( (Integer) 2, ( (AnalyticsJobParameters) jc.getJobParameters() ).getLastYears() );
    Assert.assertTrue( ( (AnalyticsJobParameters) jc.getJobParameters() ).isSkipResourceTables() );
    Assert.assertNotNull( ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes() );
    Assert.assertEquals( 0, ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes().size() );
}
 
Example 7
Source File: JobConfigurationSerializationTest.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void xmlWithJson() throws IOException
{
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
    xmlMapper.configure( DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, false );

    JobConfiguration jc = xmlMapper.readValue( "<jobConfiguration lastUpdated=\"2019-03-26T22:57:44.649\" id=\"uB9oC4R2nTn\" created=\"2019-03-26T22:57:44.649\" name=\"Test Analytic\">\n" +
        "      <jobStatus>SCHEDULED</jobStatus>\n" +
        "      <displayName>Test Analytic</displayName>\n" +
        "      <enabled>true</enabled>\n" +
        "      <leaderOnlyJob>true</leaderOnlyJob>\n" +
        "      <externalAccess>false</externalAccess>\n" +
        "      <jobType>ANALYTICS_TABLE</jobType>\n" +
        "      <nextExecutionTime>2019-03-27T12:00:00.000</nextExecutionTime>\n" +
        "      <favorite>false</favorite>\n" +
        "      <configurable>true</configurable>\n" +
        "      <access>\n" +
        "        <read>true</read>\n" +
        "        <update>true</update>\n" +
        "        <externalize>false</externalize>\n" +
        "        <delete>true</delete>\n" +
        "        <write>true</write>\n" +
        "        <manage>true</manage>\n" +
        "      </access>\n" +
        "      <lastUpdatedBy id=\"xE7jOejl9FI\"/>\n" +
        "      <jobParameters>\n" +
        "        <lastYears>2</lastYears>\n" +
        "        <skipResourceTables>true</skipResourceTables>\n" +
        "        <skipTableTypes>\n" +
        "          <skipTableType>ENROLLMENT</skipTableType>\n" +
        "          <skipTableType>ORG_UNIT_TARGET</skipTableType>\n" +
        "          <skipTableType>VALIDATION_RESULT</skipTableType>\n" +
        "        </skipTableTypes>" +
        "      </jobParameters>\n" +
        "      <cronExpression>0 0 12 ? * MON-FRI</cronExpression>\n" +
        "    </jobConfiguration>", JobConfiguration.class );

    Assert.assertEquals( JobStatus.SCHEDULED, jc.getJobStatus() );
    Assert.assertEquals( "Test Analytic", jc.getName() );
    Assert.assertEquals( "Test Analytic", jc.getDisplayName() );
    Assert.assertTrue( jc.isEnabled() );
    Assert.assertTrue( jc.isLeaderOnlyJob() );
    Assert.assertEquals( JobType.ANALYTICS_TABLE, jc.getJobType() );
    Assert.assertNull( jc.getNextExecutionTime() );
    Assert.assertEquals( "0 0 12 ? * MON-FRI", jc.getCronExpression() );

    Assert.assertNotNull( jc.getJobParameters() );
    Assert.assertEquals( (Integer) 2, ( (AnalyticsJobParameters) jc.getJobParameters() ).getLastYears() );
    Assert.assertTrue( ( (AnalyticsJobParameters) jc.getJobParameters() ).isSkipResourceTables() );
    Assert.assertNotNull( ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes() );
    Assert.assertEquals( 3, ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes().size() );
    Assert.assertThat( ( (AnalyticsJobParameters) jc.getJobParameters() ).getSkipTableTypes(), Matchers.hasItems( AnalyticsTableType.ENROLLMENT, AnalyticsTableType.ORG_UNIT_TARGET, AnalyticsTableType.VALIDATION_RESULT ) );
}
 
Example 8
Source File: ConfigLoader.java    From utah-parser with Apache License 2.0 4 votes vote down vote up
private XmlMapper buildReader() {
  XmlMapper mapper = new XmlMapper();
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, false);
  return mapper;
}
 
Example 9
Source File: MoreoverResult.java    From streams with Apache License 2.0 4 votes vote down vote up
protected MoreoverResult(String clientId, String xmlString, long start, long end) {
  this.xmlString = xmlString;
  this.clientId = clientId;
  this.start = start;
  this.end = end;
  XmlFactory xmlFactory = new XmlFactory(new InputFactoryImpl(),
      new OutputFactoryImpl());

  JacksonXmlModule module = new JacksonXmlModule();

  module.setDefaultUseWrapper(false);

  xmlMapper = new XmlMapper(xmlFactory, module);

  xmlMapper
      .configure(
          DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
          Boolean.TRUE);
  xmlMapper
      .configure(
          DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
          Boolean.TRUE);
  xmlMapper
      .configure(
          DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY,
          Boolean.TRUE);
  xmlMapper.configure(
      DeserializationFeature.READ_ENUMS_USING_TO_STRING,
      Boolean.TRUE);
  xmlMapper.configure(
      DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
      Boolean.FALSE);

  ObjectMapper mapper = new ObjectMapper();

  mapper
      .configure(
          DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
          Boolean.TRUE);
  mapper.configure(
      DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
      Boolean.TRUE);
  mapper
      .configure(
          DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY,
          Boolean.TRUE);
  mapper.configure(
      DeserializationFeature.READ_ENUMS_USING_TO_STRING,
      Boolean.TRUE);

}
 
Example 10
Source File: ResourceSyncFileLoader.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
public ResourceSyncFileLoader(CloseableHttpClient httpClient) {
  objectMapper = new XmlMapper();
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  remoteFileRetriever = new RemoteFileRetriever(httpClient);
}
 
Example 11
Source File: ResourceSyncFileLoader.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
public ResourceSyncFileLoader(RemoteFileRetriever remoteFileRetriever) {
  objectMapper = new XmlMapper();
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  this.remoteFileRetriever = remoteFileRetriever;
}