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.
Related
I defined a pipeline in Apache Beam to consume messages of a given queue in RabbitMQ message broker.
I defined an exchange and routing key in RabbitMQ.
I used AmqpIO.read() in Beam (version 2.9.0) but I did not found any API to set the echange and the routing key.
(Following this doc : https://beam.apache.org/releases/javadoc/2.4.0/org/apache/beam/sdk/io/amqp/AmqpIO.html)
Is there any possibility to do that ? Even with any other plugin.
Regards,
Ali
There is a new (experimental) IO connector for RabbitMQ shipped with the latest v2.9.0 Apache Beam release. The AMQP connector will not work for RabbitMQ.
If you are using Maven add the following dependency in your POM
<!-- Beam MongoDB I/O -->
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-mongodb</artifactId>
<version>2.9.0</version>
</dependency>
and you can use it in a pipeline like
public class RabbitMQPipeline {
final static Logger log = LoggerFactory.getLogger(RabbitMQPipeline.class);
/**
* Mongo Pipeline options.
*/
public interface RabbitMQPipelineOptions extends PipelineOptions {
#Description("Path of the file to read from")
#Default.String("amqp://localhost")
#Required
String getUri();
void setUri(String uri);
}
/**
* #param args
*/
public static void main(String[] args) {
RabbitMQPipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation()
.as(RabbitMQPipelineOptions.class);
Pipeline pipeline = Pipeline.create(options);
PCollection<RabbitMqMessage> messages = pipeline
.apply(RabbitMqIO2.read().withUri(options.getUri()).withQueue("test"));
messages.apply(ParDo.of(new DoFn<RabbitMqMessage, String>() {
#ProcessElement
public void process(#Element RabbitMqMessage msg) {
System.out.println(msg.toString());
}
}));
pipeline.run().waitUntilFinish();
}
}
The RabbitMqIO Javadoc has examples of how to use the reader and writer.
A word of caution
There is a known bug that has been fixed but scheduled for release in v2.11.0 that blocks the connector from working even in the simplest scenarios. The fix is really simple (see JIRA issue) but you will need to rebuild a new version of the class. In case you want to give it a try make sure you add the following Maven dependency
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.5.2</version>
<scope>provided</scope>
</dependency>
and add the following configuration in Maven Compiler plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessors>
<annotationProcessor>com.google.auto.value.processor.AutoValueProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
If you are using Eclipse make sure you install the m2-apt Maven plugin. Good luck!
I'm using resteasy-jaxrs in combination with jackson-datatype-jsr310 to serialize LocalDateTime in the response. This works fine for properties in my classes because I can add the necessary annotation like
#XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
public LocalDateTime getExpirationDate() {
return expirationDate;
}
using the LocalDateTimeAdapter
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
#Override
public LocalDateTime unmarshal(String s) throws Exception {
return LocalDateTime.parse(s);
}
#Override
public String marshal(LocalDateTime dateTime) throws Exception {
return dateTime.toString();
}
}
In the json I get something like
"expirationDate": "2026-07-17T23:59:59"
But I've a map of objects and this map also can have items of type LocalDateTime and for those there is no annotation and so I get the full object in the json response like
"effectiveDate": {
"dayOfMonth": 1,
"dayOfWeek": "FRIDAY",
"month": "JUNE",
"year": 2018, ...
Is there a way to format every LocalDateTime field, no matter where it comes from?
UPDATE after Paul's answer
Since I already hat the com.fasterxml.jackson.datatype:jackson-datatype-jsr310 dependency, I added the ObjectMapperContextResolver class and removed the annotation from my LocalDateTime property. Sadly all LocalDateTimes were now serialized as full object again. In the comments of the post I saw that someone added a context-param to the web.xml so that the ObjectMapperContextResolver gets picked up. After adding this to my web.xml, it looks like this.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Servlet 3.1 Web Application</display-name>
<listener>
<listener-class>com.kopi.web.InitListener</listener-class>
</listener>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.kopi.utils.ObjectMapperContextResolver</param-value>
</context-param>
</web-app>
Now since adding the context-param resteasy.resources, the webapp doesn't start anymore due to
SEVERE: Servlet [com.kopi.rest.RestApplication] in web application [/kopi] threw load() exception
java.lang.RuntimeException: RESTEASY003130: Class is not a root resource. It, or one of its interfaces must be annotated with #Path: com.kopi.utils.ObjectMapperContextResolver implements: javax.ws.rs.ext.ContextResolver
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:179)
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:158)
at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:77)
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:482)
at org.jboss.resteasy.spi.ResteasyDeployment.startInternal(ResteasyDeployment.java:279)
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:86)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:119)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1194)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1110)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1000)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4902)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5212)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
In other comments I saw that versions might be an issue. So I'm currently using
org.jboss.resteasy:resteasy-jaxrs 3.5.1.Final
org.jboss.resteasy:resteasy-jackson-provider 3.5.1.Final
org.jboss.resteasy:resteasy-servlet-initializer 3.5.1.Final
com.fasterxml.jackson.datatype:jackson-datatype-jsr310 2.9.5
Thank you, kopi
Instead of the XmlAdapter which you need to declare on individual properties, you could just configure it globally with Jackson using a ContextResolver. This is where you can configure the ObjectMapper and register the JavaTimeModule with the mapper. This configuration will be global so you don't need to use the XmlAdapter. To see how you can configure this, you can see this post.
#XmlJavaTypeAdapter can apply to a whole package (see doc).
When #XmlJavaTypeAdapter annotation is defined at the package level it
applies to all references from within the package to
#XmlJavaTypeAdapter.type().
Try to add the file package-info.java next to the class that has the map. Given the package of the class is com.acme.beans and that of the adapter is com.acme.adapters, the content of the file should be something like:
#XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
package com.acme.beans;
import com.acme.adapters.LocalDateTimeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
I am able to deploy a RESTEasy application working well with Weld (meaning my CDI works) but I am having some trouble with my integration tests. I get this error:
org.jboss.weld.exceptions.DeploymentException:
WELD-001408: Unsatisfied dependencies for type SomeService with qualifiers #Default
while testing:
#RunWith(WeldJUnit4Runner.class)
public class SomeServiceIT {
#Inject
private SomeService service;
#Test
public void test() {
System.out.println(service);
}
}
The last message in my logs is
DEBUG::WELD-000100: Weld initialized. Validating beans
Content of src/test/resources/META-INF/beans.xml:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
By the way I tried the cdi-unit library and it works, but I need to use my own WeldJUnit4Runner which is currently:
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
private final Weld weld;
private final WeldContainer container;
public WeldJUnit4Runner(Class<?> klass) throws InitializationError {
super(klass);
this.weld = new Weld();
this.container = weld.initialize();
}
#Override
protected Object createTest() throws Exception {
return container.instance().select(getTestClass().getJavaClass()).get();
}
}
I use weld-se 2.4.1.Final for testing.
Thanks.
EDIT:
So it seems like Weld only looks into src/test/java (when I copy SomeService over to src/test/java it woks). This is silly, I am not going to duplicate all my classes to test them... How to tell Weld to retrieve classes from src/main/java?
So I was able to make it work by creating src/main/resources/META-INF/beans.xml in addition to the existing src/main/webapp/WEB-INF/beans.xml and src/test/resources/META-INF/beans.xml meaning now I have 3 times the exact same file in the same project which I find silly but I guess this is how it is in the Weld world...
Thanks all for your time.
EDIT:
Actually I am able to deploy the application with only src/main/resources/META-INF/beans.xml (I removed src/main/webapp/WEB-INF/beans.xml)
Sorry, I have no solution, but only a small clue: if you want to do some customizations of the BlockJUnit4ClassRunner - why don't you try to extend the org.jglue.cdiunit.CdiRunner or org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner? Or at least take a look at their source code.
Ps. I always find Weld's class-path scanning brittle & error prone. And try to avoid it as much as possible.
It should work so I post here what I did.
Firstly, I use :
Eclipse Luna
JDK 7
The tree of my project is the following one :
Here are my pom dependencies :
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>2.4.1.Final</version>
<scope>test</scope>
</dependency>
The SomeService interface :
package org.jvi.cdirunner;
public interface SomeService {
void test();
}
The SomeServiceImpl implementation :
package org.jvi.cdirunner;
public class SomeServiceImpl implements SomeService {
#Override
public void test() {
// TODO Auto-generated method stub
}
}
And the test to run :
package org.jvi.cdirunner.test;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvi.cdirunner.SomeService;
#RunWith(WeldJUnit4Runner.class)
public class SomeServiceIT {
#Inject
private SomeService service;
#Test
public void test() {
System.out.println(service);
}
}
And everything works fine if I run the test under Eclipse. I can't figure out why it doesn't work on your side.
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);
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