Jackson 2.4.2 Date serialization not milliseconds - spring

I'm using Spring MVC 3.2.x with Jackson 2.4.2 for JSON web services.
I have objects that contain java.util.Date and the JSON contains a string representation of just the date portion: ("2014-09-15"). this goes against the Jackson documentation that says dates by default get marshalled as milliseconds epoch format (http://wiki.fasterxml.com/JacksonFAQDateHandling).
I would like the date members to be returned as milliseconds format, what am I missing here?
Here is my jackson libraries in my pom file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
I have annotation-driven in my applicationContext:
Thanks!
Alessandro Ferrucci

In my case, problem was in Spring Data REST(2.2.1) disabling WRITE_DATES_AS_TIMESTAMPS by default. I am sure Spring MVC might be doing the same, but I unable to locate that code-commit.
However I was able to locate code-commit in case of Spring Data REST:
DATAREST-336 - Default to ISO8601 date rendering
https://github.com/spring-projects/spring-data-rest/commit/2f1e9824cddb6085c9fd86f1e0b84721497669bb

Related

Stop sending Spring boot metrics to Prometheus with Micrometer

I have a Spring boot application where I am sending custom metrics generated within the service to Prometheus via Pushgateway.
I am using Prometheus Pushgateway with Micrometer, mainly based on this tutorial: https://luramarchanjo.tech/2020/01/05/spring-boot-2.2-and-prometheus-pushgateway-with-micrometer.html
I have following dependencies in my pom.xml
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
And sending custom metrics with:
Counter counter = Counter.builder("sb_console_test_counter").register(meterRegistry);
counter.increment();
It is working fine and I can view the custom metrics generated by the application however in addition to this I am seeing application specific metrics generated by Spring boot e.g.
tomcat_sessions_active_current_sessions
tomcat_sessions_active_max_sessions
etc.
I only want to capture the custom metrics generated by my code and not any other generic metrics, how can I stop sending this?
When you add the dependency spring-boot-starter-actuator you will get a lot of metrics out of the box from various configurations such as JvmMetricsAutoConfiguration and TomcatMetricsAutoConfiguration.
To filter those out, you could add a deny filter in your Micrometer config, and only allow your custom metric meters to be registered.
Example using a deny filter:
#Bean
public MeterRegistryCustomizer<MeterRegistry> metricsRegistryConfig() {
return registry -> registry.config()
.meterFilter(MeterFilter.deny(id -> !id.getName().startsWith("sb_console")));
}
The above will deny any metrics not starting with sb_console.
See this link for more info about the meter filters
I believe you can just add this:
management.metrics.export.defaults.enabled=false
to your application.properties. I see this in some of my code. I'm not set up to try it right now, but that's why we have it there.
If you want to add back some of the built in metrics, you can then add lines like this:
management.metrics.export.<groupname>.enabled=true

#XmlTransient not working for Json

I am creating a simple API using Jersey Jax-Rs. I am using #XmlTransient to ignore the POJO being displayed in the output for a 1-to-many relationship. It works fine for XML output but the POJO gets displayed while outputting JSON. Do I need to add any dependency?
I am using the following dependency:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
If you are using json-media-json-binding.jar , There is an annotation "#JsonbTransient" which helped me achieve this.

How use java.time.LocalDate using Hibernate5 and Vaadin8?

I'm new on Vaadin and I'm trying to do a simple crud application.
I was starting from Vaadin CRUD sample; then I modify the backend project to use Hibernate and the ui project to manage the various tables.
I can do that for simple data type like varchar and int, now I would add a date's field but it returns me this error:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE1\x06\x1Ax' for column 'data_nascita' at row 1
"DATA_NASCITA" is a date column on my mysql DB and the entity has a LocalDate linked to this field.
I found somewhere that this new java api is compatible only with Hibernate5 so in my pom file I add:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
and into the entity class I replace java.util.Date with java.time.LocalDate and delete the annotation #temporal.
The input class for this field is a DateField of Vaadin framework.
Which could be the problem?
P.S.: I'm blind, so I'm compiling the code with Netbeans8 but I modify the code with notepad++ so, if someone use the same technologies and hasn't any problem please tell me because I can't be sure that the problem is something with build process.
IF your hibernate version is greater than 5.2.+ you can just using column as below:
#Column
private LocalDate date;
Otherwise you need install hibernate-java8 module into your project.
On the other hand, you shouldn't install hibernate-java8 module into your project if the hibernate version >= 5.2, maybe it will makes some conflict, since the hibernate-java8 module is merged into hibernate.

Spring Data Projection not working

I want to use spring projection in my project. I am doing exactly as mentioned at this url http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
interface NoAddresses {
String getFirstName();
String getLastName();
}
The only difference is my interface is public. and is not in the same package as the repository. Is that a requirement?
Still I see the whole entities are being returned, instead of just the projected columns.
I have
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
Doesn't work. Do i need to add some dependency to make the projection work? I am not using spring-boot but just the spring-data stuff in my project, running on tomcat server.
thanks
Chahat

LocalDateTime Serialization in Spring Boot 1.4 Release returns array

Recently I updated my project to use Spring Boot 1.4-Release, however, the serialization of LocalDateTime seems to be broken.
With below pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
...
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
....
and Jackson configuration in application.properties
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
LocalDateTime object is formatted as an array, something like [2016, 8, 17, 11, 50, 0], as opposed to "2016-08-17T11:50:00" which was what I got when using Spring Boot 1.4.RC1
However, LocalDate can still be correctly interpreted to "2016-08-17".
Can anyone help? Much appreciated!!
Thanks all! It's actually my bad ... in order to reduce the network traffic time, the result is serialized and then compressed before transmission, however, I forgot to set the WRITE_DATES_AS_TIMES‌​TAMPS feature to false in the manually created ObjectMapper

Resources