WRITE_DATES_WITH_ZONE_ID cannot be disabled for ZonedDateTime - jackson

I'm using Jackson 2.8.1 for serialization of java object. However, I just can't git rid of zone id when converting a ZonedDateTime object to a string with "WRITE_DATES_WITH_ZONE_ID" set to false
ObjectMapper mapper = new ObjectMapper()
.findAndRegisterModules()
.setSerializationInclusion(Include.NON_EMPTY)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(SerializationFeature.WRITE_DATES_WITH_ZONE_ID, false);
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(mapper.writeValueAsString(zdt)); // "2016-08-23T13:35:38.127+08:00[Asia/Shanghai]"
Can any one help?

I would say the issue is because you call "findAndRegisterModules".
You probably added the following dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
Doing so, you introduced two new modules: "JSR310Module" (deprecated) and "JavaTimeModule".
"findAndRegisterModules" can register the "JSR310Module" that is not supporting properly the WRITE_DATES_WITH_ZONE_ID feature.
You can register the right module by removing "findAndRegisterModules" and adding:
JavaTimeModule module = new JavaTimeModule();
registerModule(module);
Then, don't forget to disable WRITE_DATES_WITH_ZONE_ID in your mapper:
disable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);

Related

HttpComponentsClientHttpConnector is not accepting org.apache.http.impl.nio.client.CloseableHttpAsyncClient for Webclient with Apache Http Client

Im trying to run Webflux on Tomcat and try to create Sping WebClient with Apache Http Client.
Reference Documentation stated that theres built-in support:
https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-client-builder-http-components
private ClientHttpConnector getApacheHttpClient(){
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
clientBuilder.setDefaultRequestConfig(RequestConfig.DEFAULT);
CloseableHttpAsyncClient client = clientBuilder.build();
ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);
return connector;
}
But Springs HttpComponentsClientHttpConnector is not accepting org.apache.http.impl.nio.client.CloseableHttpAsyncClient. It requires org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient. So there seems to be a package rename and I canĀ“t find a Maven Dependency that has the required class.
Does anybody know the right Maven Dependency for that class. Or how could I make it work?
Apache HTTP Client 5 is a separate artifact. You'll need to add the following dependencies to your pom.xml:
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5-reactive</artifactId>
<version>5.1</version>
</dependency>
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.springframework.http.client.reactive.HttpComponentsClientHttpConnector;
public class ApacheHttp {
public static void main(String[] args) {
new HttpComponentsClientHttpConnector(HttpAsyncClients.custom().build())
}
}

spring.jackson properties not found

In my Spring Boot project it seems none of the spring.jackson properties are working. I want to take controll over date fields and I tried to add the following properties to my application.properties file:
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.joda-date-time-format=yyyy-MM-dd HH:mm:ss
Dates still come as timestamps (java.util.Date and joda.time). I have the following dependencies:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
I didn't add version number but it says the managed version number is 2.8.7 (with all three).
I've read if I use #EnableWebMvc annotation on any class, it won't work. But I don't use such annotation. (However I have spring-webmvc on my classpath.)
When I start the application (in Tomcat), I got these log messages:
2017-04-23 16:54:55 DEBUG JndiTemplate:150 - Looking up JNDI object with name [java:comp/env/spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS]
2017-04-23 16:54:55 DEBUG JndiLocatorDelegate:101 - Converted JNDI name [java:comp/env/spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS] not found - trying original name [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS]. javax.naming.NameNotFoundException: Name [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS] is not bound in this Context. Unable to find [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS].
2017-04-23 16:54:55 DEBUG JndiTemplate:150 - Looking up JNDI object with name [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS]
2017-04-23 16:54:55 DEBUG JndiPropertySource:99 - JNDI lookup for name [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS] threw NamingException with message: Name [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS] is not bound in this Context. Unable to find [spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS].. Returning null.
2017-04-23 16:54:55 DEBUG PropertySourcesPropertyResolver:151 - Found key 'spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS' in [URL [file:C:/Program Files/Apache Software Foundation/Tomcat 9.0/conf/Catalina/localhost/Blowfish/config/application.properties]] with type [String]
Only one thing worked with both java.util.Date and joda.time:
#Configuration
public class AppConfig extends WebMvcConfigurerAdapter{
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
break;
}
}
}
}
But I don't like this solution, it would be more elegant to use some configuriation in my application.properties.
I use Spring Boot 1.5.2.

Infinispan 6.0 CDI and default configuration

I'm having problems defining configuration for a CDI application (glassfish 4).
I have:
#CacheResult(cacheName = "example")
public String getSomething(String something){
logger.debug("getSomething "+something);
return "this is "+something;
}
This works as expected, the second time is called is not executed because it's cached
However, I want to specify a configuration for my caches. I have tried writing a infinispan.xml file (in src/main/resources), but it's ignored. I have also tried with both:
#Produces
#Default
public Configuration defaultEmbeddedCacheConfiguration() {
return new ConfigurationBuilder().expiration().lifespan(3000l)
.eviction()
.strategy(EvictionStrategy.LRU)
.maxEntries(2)
.build();
}
#Produces
#ApplicationScoped
public EmbeddedCacheManager defaultEmbeddedCacheManager() {
return new DefaultCacheManager(defaultEmbeddedCacheConfiguration());
}
But these methods are never called.
I have also tried with #ConfigureCache
My dependencies are:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-cdi</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-jcache</artifactId>
<version>6.0.2.Final</version>
</dependency>
Any ideas?
Thx

Force Glassfish4 to use Jackson instead of Moxy

Glassfish4 is using Moxy to serialize REST responses into JSON. Does anybody know how to configure application to use Jackson instead of Moxy?
You need to register JacksonFeature in your application if you want to use Jackson as your JSON provider (by registering this feature your disable MOXy to be your JSON provider).
You can do it either in Application subclass:
public class MyApplication extends Application {
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// Add root resources.
classes.add(HelloWorldResource.class);
// Add JacksonFeature.
classes.add(JacksonFeature.class);
return classes;
}
}
or in ResourceConfig:
final Application application = new ResourceConfig()
.packages("org.glassfish.jersey.examples.jackson")
.register(MyObjectMapperProvider.class) // No need to register this provider if no special configuration is required.
// Register JacksonFeature.
.register(JacksonFeature.class);
See Jackson section in Jersey Users Guide for more information.
Answer by Michal Gajdos is correct, just to add to that, add this dependency in your pom.xml ,
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.26</version>
</dependency>

Is it possible to start Mule3 embedded without spring dependency?

I am trying to use Mule 3.2.1 embedded from a plain java application. The application is suppose to run in an environment where storage space is limited.
I tried something like (import, exceptions omitted for brevity):
DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
ConfigurationBuilder configBuilder = new AutoConfigurationBuilder("mule-config.xml");
MuleContext muleContext = muleContextFactory.createMuleContext(configBuilder);
muleContext.start();
and also this:
AutoConfigurationBuilder configBuilder = new AutoConfigurationBuilder("mule-config.xml");
DefaultMuleConfiguration configuration = new DefaultMuleConfiguration();
MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
contextBuilder.setMuleConfiguration(configuration);
MuleContext muleContext = new DefaultMuleContextFactory().createMuleContext(configbuilder, contextBuilder);
muleContext.start();
but both require spring-core, spring-beans, spring-context and some commons libraries. Any help would be great.
If you use the XML configuration, you need Spring.
If you don't want to use Spring, your options are:
Instantiate and wire Mule internal components by hand, dealing with life cycles as well,
Wait until Mule DSL gets released. You may want to bug MuleSoft about a release date :)
If you only want to use raw transports, ie not configure any flow or pattern, you can do it without Spring but bear in mind that, if the mule-core dependency doesn't bring Spring transitively, all the modules and transports do. This means that you'll have to use filtering to keep these dependencies at bay.
For example to use the HTTP transport, you would need these Maven dependencies:
<dependency>
<groupId>org.mule</groupId>
<artifactId>mule-core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-http</artifactId>
<version>3.4.0</version>
<exclusions>
<exclusion>
<groupId>org.mule.modules</groupId>
<artifactId>mule-module-spring-config</artifactId>
</exclusion>
</exclusions>
</dependency>
With this in place you then do:
MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder();
MuleContext muleContext = muleContextFactory.createMuleContext(muleContextBuilder);
muleContext.start();
MuleClient client = muleContext.getClient();
MuleMessage response = client.request("http://www.google.com", 20000L);
System.out.println(response.getPayloadAsString());
muleContext.dispose();
System.exit(0);
Note that if that's all you're doing with Mule, then you'd rather use the Apache HTTP Client directly :)
MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder();
MuleContext muleContext
muleContextFactory.createMuleContext(muleContextBuilder);
muleContext.start();
// create mule client
MuleClient client = new MuleClient(muleContext);
// generate xml request
String reportRequestXml = createXML(reportRequest);
// set up message properties
Map<String, Object> messageProperties = new HashMap<String, Object>();
messageProperties.put("Content-Type", "application/xml");
// send request with timeout
MuleMessage response = client.send(crsRestUrl, reportRequestXml, messageProperties, httpTimeout);
muleContext.stop();