0

Before adding the jackson-annotations maven dependency this method returned the date with the expected format:

@GET
@Path("/date")
@Produces(MediaType.APPLICATION_JSON)
public Response getDate() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
    LocalDate birthDateOwner = LocalDate.parse("1968.03.21", formatter);
    return Response.ok().entity(birthDateOwner).build();
}

Response

"1968-03-21"

After adding this dependency the format of the responded date changed

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.12.1</version>
</dependency>

Response

{"year":1968,"month":"MARCH","era":"CE","dayOfMonth":21,"dayOfWeek":"THURSDAY","dayOfYear":81,"leapYear":true,"monthValue":3,"chronology":{"id":"ISO","calendarType":"iso8601"}}

How can I get the date in the first format?

1 Answers1

0

Two things needed to be done. First added new dependency to pom xml

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.12.1</version>
        </dependency>

And a new class for objectmapper configuration

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
    private final ObjectMapper mapper;

    public ObjectMapperContextResolver() {
        mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }
}

Here how it looks from curl

$ curl -v http://localhost:8080/DateFormatProblem-1.0-SNAPSHOT/rs/dates/date
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying ::1:8080...
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /DateFormatProblem-1.0-SNAPSHOT/rs/dates/date HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.75.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: CSRF-Token, X-Requested-By, Authorization, Content-Type
< Access-Control-Allow-Credentials: true
< Content-Type: application/json
< Content-Length: 12
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
< Date: Thu, 18 Feb 2021 20:55:26 GMT
<
{ [12 bytes data]
100    12  100    12    0     0     52      0 --:--:-- --:--:-- --:--:--    52"1968-03-21"
* Connection #0 to host localhost left intact

I have another suggestion, which is start using spring boot or quarkus :) testing will be far more easy and you can find a lot of documentation about more details. check https://stackoverflow.com/a/29959842/175554

ozkanpakdil
  • 3,199
  • 31
  • 48