Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39926
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Java record |
| Date | 2023-03-08 20:33 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <tuanur$125a7$1@dont-email.me> (permalink) |
| References | <906f5a76-929d-46cb-b5e0-fcc8e4ad4e0en@googlegroups.com> |
On 01/03/2023 20:18, e.d.pro...@gmail.com wrote:
> I just saw they added a new thing called record to Java 16. Is this not a replacement for a DTO or am I missing something?
> I create a DTO class like:
> public class TestDTO {
> private String testField;
> public String getTestField() {
> return this.testField;
> }
> public void setTestField(String test) {
> this.testField = test;
> }
> }
>
> I define a matching record definition like:
> public record TestRecordDTO(String testField) {
>
> }
>
> Now I try to test them:
> public static void main(String[] args) {
> TestRecordDTO rec = new TestRecordDTO("testrec");
>
> TestDTO dto = new TestDTO();
> dto.setTestField("testrec");
>
> Gson gson = new Gson();
> System.out.println(gson.toJson(rec)); // {"testField":"testrec"}
> System.out.println(gson.toJson(dto)); // {"testField":"testrec"}
> rec.testField();
> dto.getTestField();
>
> dto = gson.fromJson(gson.toJson(dto), TestDTO.class);
> System.out.println(dto.getTestField()); // testrec
> rec = gson.fromJson(gson.toJson(rec), TestRecordDTO.class); // throws AssertionError
> System.out.println(rec);
> }
>
> In web, we use DTOs on the back end to pass data to and from the Javascript/Typescript on the front end. It looks like passing to the front end works, now how do we use it as a model to get the user input?
Note: if you do get an exception, you should at least state its message
in your post.
According to the changelog, support for Java records has been added to
Gson in version 2.10:
<https://github.com/google/gson/releases?q=2.10&expanded=true>. Switch
to that and you should be good to go.
--
DF.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Java record "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-03-01 11:18 -0800
Re: Java record Arne Vajhøj <arne@vajhoej.dk> - 2023-03-01 20:23 -0500
Re: Java record "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-03-02 04:01 -0800
Re: Java record Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2023-03-08 20:33 +0100
Re: Java record "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-03-08 12:14 -0800
Re: Java record Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2023-03-08 21:44 +0100
Re: Java record "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-03-08 12:54 -0800
Re: Java record Arne Vajhøj <arne@vajhoej.dk> - 2023-03-13 10:55 -0400
Re: Java record "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-03-13 08:25 -0700
Re: Java record Jason H <DarthPiriteze@deathstar.gal> - 2023-03-21 18:02 +0000
Re: Java record "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-03-22 04:04 -0700
csiph-web