Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39938

Re: Java record

From Jason H <DarthPiriteze@deathstar.gal>
Newsgroups comp.lang.java.programmer
Subject Re: Java record
Date 2023-03-21 18:02 +0000
Organization A noiseless patient Spider
Message-ID <tvcrgf$6po8$2@dont-email.me> (permalink)
References <906f5a76-929d-46cb-b5e0-fcc8e4ad4e0en@googlegroups.com>

Show all headers | View raw


On 3/1/23 19: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?

I encountered this when I Googled how to do structs in Java.  The answer 
was not before 16 (though really, a class with a bunch of public 
variables will do, if that's what you need).

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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