0
I'm using Quarkus version 1.5.1, however when trying to send data of type LocalDate (or Date) of a form, it returns the following error message:
java.lang.RuntimeException: RESTEASY007545: Unable to find a MessageBodyReader for media type: text / plain; charset = us-ascii and class type java.time.LocalDate
I have already imported the following dependencies, but the error persists.
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId> io.quarkus </groupId>
<artifactId> quarkus-resteasy </artifactId>
</dependency>
<dependency>
<groupId> io.quarkus </groupId>
<artifactId> quarkus-resteasy-jsonb </artifactId>
</dependency>
<dependency>
<groupId> io.quarkus </groupId>
<artifactId> quarkus-resteasy-jaxb </artifactId>
</dependency>
<dependency>
<groupId> io.quarkus </groupId>
<artifactId> quarkus-resteasy-jackson </artifactId>
</dependency>
Here is the code:
`
@POST
@Consumes (MediaType.MULTIPART_FORM_DATA)
@Produces (MediaType.APPLICATION_JSON)
@Transactional
@Path ("/ new")
public Response addSessao (@MultipartForm @Valid Sessao sessao) {
sessionService.insert (session);
return Response.seeOther (URI.create ("/ sessions")). build ();
}
`
`
@Column (name = "data_inicio_sessao")
@FormParam ("data_inicio_sessao")
public LocalDate dataInicioSessao;
@Column (name = "data_fim_sessao")
@FormParam ("data_fim_sessao")
public LocalDate dataFimSessao;
`
`
<div class="form-group">
<label class="sr-only" for="data_fim_sessao">Data Fim</label>
<input type="text" name="data_fim_sessao" class="form-control" id="data_fim_sessao" placeholder="Data Fim" required autofocus {#if update}value="{sessao.dataFimSessao}"{/if}>
</div>
`
*Note: If I change the field type from LocalDate to String the data is saved in the database correctly.
However, I noticed that if you put text with accents, the encoding is wrong.
I tried to change the settings of the H2 datasource, but I couldn't.
@asoldano is something more needed for java.time? Or does it need to be done differently?
Hi @asoldano, thanks for the feedback.
I've tried it in 2 formats (Date and LocalDate), but none of the 2 worked.
Returns this error: java.lang.RuntimeException: RESTEASY007545
I already searched the internet, but I couldn't find a solution for that.
I commented on stackoverflow the other day regarding this exact topic (https://stackoverflow.com/questions/62420547/how-to-solve-java-lang-runtimeexception-resteasy007545-unable-to-find-a-messag ), but my answer was deleted by another user as it was question in response to a question. Anyway, I wrote:
Are you using a JAX-RS ParamConverter for converting Dates to/from Strings? You can see an example at https://github.com/resteasy/Resteasy/blob/master/testsuite/integration-tests/src/test/java/org/jboss/resteasy/test/resource/param/resource/DateParamConverter.java
@asoldano thanks for the feedback.
I had already tried with a similar class to convert dates, but with the type LocalDate.
I tried now with this class that you passed, but it also didn't work. He returned the following error message:
java.lang.IllegalArgumentException: RESTEASY003245: Date instances are not supported by this class.
I tried it this way now, and it didn't work either:
@JsonFormat (pattern = "yyyy-MM-dd HH: mm: ss", timezone = "UTC")
@Column (name = "data_inicio_sessao")
@FormParam ("data_inicio_sessao")
public Instant dataInicioSession;
The previous error returned again:
java.lang.RuntimeException: RESTEASY007545: Unable to find a MessageBodyReader for media type: text / plain; charset = us-ascii and class type java.time.Instant
There is something very wrong with this Quarkus Framework that is not letting the date field accept the entity at all.
I already tried putting types Date, LocalDate and now Instant, using this annotation "JsonFormat" and nothing works.
Looks like the ParamConverters are being ignored when the @FormParam is used inside the annotated MultiPartForm param.
I was able to stop getting this error by creating a MessageBodyReader:
@Provider
@Produces(MediaType.TEXT_PLAIN)
public class LocalDateReader implements MessageBodyReader<LocalDate> {
@Override
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return type == LocalDate.class;
}
@Override
public LocalDate readFrom(Class<LocalDate> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> multivaluedMap, InputStream inputStream) throws IOException, WebApplicationException {
return LocalDate.parse(new String(inputStream.readAllBytes()));
}
}
Hi @marcelorubim
Yes, I identified that the problem is in "@FormParam", but if I take this note there I can not get the value sent by the form to save in the database.
I didn't quite understand what you suggested doing, but I tried to extend this class, but it was wrong:
return LocalDate.parse (new String (inputStream.readAllBytes()));
Quarkus is unable to interpret "inputStream.readAllBytes()".
Can you tell me if you need to import an external lib?
Hi, @danilomeneghel!
No, you do not need any import.
inputStream is one of the parameters from the function readFrom
OK.
Yes, but this code above did not work.
Does not recognize inputStream.readAllBytes()
Well, are you using which Java version?
If its 8, you should migrate to Java 11. This codes worked on Java 11.
https://docs.oracle.com/javase/9/docs/api/java/io/InputStream.html
It worked!
Thank you very much @marcelorubim.
It was the version of Java that had to change.
In my pom file it was still in version 1.8.
I moved now to version 14 of Java and it worked just fine.