3

I trying to use the Resteasy CLIENT to call one REST service

In my Service I create with springboot and return one LocalDateTime propert

If I use this depencency

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

This is my code in my bean:

private LocalDateTime dtPublicacao;

this is the result when I call my service:

dtPublicacao: "2017-04-20T00:00:00"

if I remove this the result:

dtPublicacao: {
  hour: 0,
  minute: 0,
  second: 0,
  nano: 0,
  dayOfYear: 110,
  dayOfWeek: "THURSDAY",
  month: "APRIL",
  dayOfMonth: 20,
  year: 2017,
  monthValue: 4,
  chronology: {
    id: "ISO",
    calendarType: "iso8601"
  }
}

So in my Client I create the same model and use this to execute the Get

Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:8585").path("/edital/");
        try{
            List<EditalVO> response = target.request().get(new GenericType<List<EditalVO>>(){});
            return response;
        }catch (NotFoundException e) {          
            throw new NotFoundException();
        }       

So, if I put in my client the LocalDateTime I got this error:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method (through reference chain: br.com.lumera.protesto.edital.vo.EditalVO["dtExpiracao"])

to solve I need to change in my client form LocalDateTime to Date and add

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date dtPublicacao;

I Already try to add the same dependency in my client, but he register the LocalDateTimeSerializer.class but when I call the REST he dot no go through the serialize method and I got the error again.

Can I RECEIVE AND SEND LocalDateTime from my Restasey CLIENT ??

tks

UPDATE I already try to does this too:

ObjectMapper obj = new ObjectMapper();
obj.registerModule(new JavaTimeModule())

with no success, then I try to in my client :

client.register(obj);

with no success to.. if I debug my app, he enter on methods

public LocalDateTimeSerializer(DateTimeFormatter f) {
    super(LocalDateTime.class, f);
}

private LocalDateTimeSerializer(LocalDateTimeSerializer base, Boolean useTimestamp, DateTimeFormatter f) {
    super(base, useTimestamp, f);
}

in LocalDateTimeSerializer but don`t enter in serialize method or deserialize

Fabio Ebner
  • 2,613
  • 16
  • 50
  • 77
  • the [javadoc](https://docs.jboss.org/resteasy/docs/3.0.9.Final/javadocs/javax/ws/rs/core/Configurable.html#register(java.lang.Object)) says: register an extension provider or a feature meta-provider instance can be used in the scope of this context. – holi-java Apr 26 '17 at 15:03

2 Answers2

1

Not entirely sure, but seems like you need to have this dependency

and register the module via

objectMapper.registerModule(new JavaTimeModule());
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • yes, you are right. the test has been assert that you must register the [JavaTimeModule](https://github.com/FasterXML/jackson-datatype-jsr310/blob/master/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java#L14) before [deserialization](https://github.com/FasterXML/jackson-datatype-jsr310/blob/master/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestLocalDateTimeDeserialization.java#L18). – holi-java Apr 26 '17 at 09:59
  • @holi-java you took the time to look at the actual tests, not bad! – Eugene Apr 26 '17 at 10:01
  • I believe the first-line programmers when I'm not sure how to use the library, It will not take much time just a few minutes if the intent is clearly . – holi-java Apr 26 '17 at 10:06
  • Hi, Eugene. and the pattern of the `@JsonFormat` of the OP should be `"yyyy-MM-dd'T'HH:mm:ss"` in client side. – holi-java Apr 26 '17 at 10:13
  • @Eugene I already try to does this.. I will update my question with my test – Fabio Ebner Apr 26 '17 at 12:00
  • @Eugene I not using spring or another framework so how is the corretly way to register? tks – Fabio Ebner Apr 26 '17 at 12:08
  • @FabioEbner how are getting a hold of `ObjectMapper` then? – Eugene Apr 26 '17 at 12:16
  • @Eugene I don`t understand your question – Fabio Ebner Apr 26 '17 at 12:39
  • @FabioEbner doing this `ObjectMapper obj = new ObjectMapper();` has no effect. You need the `ObjectMapper` that is currently in use. Where are you deploying your application? U said u are not using `Spring`, but what then? Plain Servlets? How are you exposing your REST Apis? – Eugene Apr 26 '17 at 13:55
  • @Eugene :), he used resteasy-client module, I think he don't know how to config `ObjectMapper` into the module. – holi-java Apr 26 '17 at 14:23
0

How to config ObjectMapper into resteasy-client module? you can see this question.


I have wrote two tests how one side to fit another side on github.

fit client side to server side

If you want to the client side to fit the server side you must set the pattern DateFormat to "yyyy-MM-dd'T'HH:mm:ss", for example:

ObjectMapper#setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"))

OR you can using @JsonFormat as:

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Date dtPublicacao;

fit server side to client side

If you want to the server side to fit the client side you must set the pattern DateTimeFormatter to "yyyy-MM-dd HH:mm:ss", for example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Module module = new JavaTimeModule().addDeserializer(LocalDateTime.class
                                      ,new LocalDateTimeDeserializer(formatter));

ObjectMapper#registerModule(module);
Community
  • 1
  • 1
holi-java
  • 29,655
  • 7
  • 72
  • 83