Can I use a java class inside protobuf file? - java

I have two java classes, called MyJavaClass and MyAnotherJavaClass.
MyJavaClass :
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class MyJavaClass implements Serializable {
private Integer id;
private String name;
private MyAnotherJavaClass myAnotherJavaClass;
}
MyAnotherJavaClass:
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class MyAnotherJavaClass implements Serializable {
private Integer id;
private String someField;
}
Can I have something like this?
message MyClassMessage {
MyJavaClass javaclass = 1;
}
Instead of create a message to use protobuf, since I use the same fields.

Related

How can I make javax validation both of sub class and super class?

I'm using Spring framework,
and I faced the inheritance problem when I write Controller logic.
First of all,
this is my Controller code snippet
#PostMapping("/pay/detail")
public ResponseEntity<PayDetail.Response> getPayDetail(
#Valid PayDetail.Request payDetail
) {
... some code
}
and PayDetail class looks like this
public class PayDetail {
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public static class Request extends CommReqForm {
#NotNull(message = "Not null please")
private String work_type;
}
}
and CommReqForm
#Data
#AllArgsConstructor
#NoArgsConstructor
public class CommReqForm {
#NotEmpty(message = "servicecode not empty")
private String servicecode;
#NotEmpty(message = "reqtype not empty")
private String reqtype;
}
I wish that I can validate both of PayDetail.Request and CommReqForm classes but It makes validation just only PayDetail.Request class.
How can I solve this problem?
#Valid cannot validate super class. I want to make both of sub class and super class validation.

How can I use JAXB to bind mulitple non-unique elements

I'm having this issue with parsing XML using JAXB. Here is a simplified layout of the XML in question:
<message>
<header>
<network>NET</network>
<sendTime>0722</sendTime>
</header>
<generalInformation>
<senderReference>1234</senderReference>
<linkage>
<externalReference>extRef</externalReference>
</linkage>
<linkage>
<internalReference>intRef</internalReference>
</linkage>
<linkage>
<systemReference>sysRef</externalReference>
</linkage>
</generalInformation>
</message>
The problem I'm having is that these references are being sent under the linkage tag which is not unique, and also doesn't have a root like "linkages" which would allow me to easily wrap it in a list in Java, because the generalInformation tag has other tags in it. Here is how I have it set up so far:
#XmlRootElement(name="message")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Message {
private Header header;
private GeneralInformation generalInformation;
}
#XmlRootElement(name="header")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Header {
private String network;
private String sendTime;
}
#XmlRootElement(name="generalInformation")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class GeneralInformation {
private String senderReference;
//How to create for linkages??
}
So my question to you is, how can I configure the GeneralInformation class to handle these multiple linkages? I am mostly concerned with unmarshalling from XML to Java at the moment.
Just define it as a List, for example:
private List<Linkage> linkage;
and define the Linkage class to have single String property:
#XmlRootElement(name="linkage")
...
public class Linkage {
private String systemReference;
}
#XmlRootElement(name="generalInformation")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class GeneralInformation {
private String senderReference;
private List<Linkage>;
}
#XmlRootElement(name="linkage")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Linkage {
private String externalReference;
private String internalReference;
private String systemReference;
}

Does #AllArgsConstructor create constructor for static member?

For this class will #AllArgsConstructor create field for count?
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Money {
private int paisa;
private int rs;
private static int count;
}
No, it will not.
See here: https://projectlombok.org/features/constructor
Static fields are skipped by these annotations.

Getters and Setters in Lombok when serializing

I have this inner class defined in a service
#RunWith(SpringRunner.class)
#SpringBootTest
public class HostelIntegrationTest {
#Data
#Builder
#NoArgsConstructor
#Getter
#Setter
#AllArgsConstructor
#JsonInclude(NON_NULL)
#EqualsAndHashCode
static class User {
String property1;
Instant property2;
Integer property3;
}
but when I serialize the class
User user = User.builder().property1("property1").property2(Instant.now()).property3(5).build();
JSONSerializer.toJSON(user))
I got this error:
net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'property1' has no getter method in class..

Lombok builder override default constructor

I was setting the value of recordId from the child classes using the default constructor and was not using lombok #Builder initially. Eventually i decided to use the Builder here, but the problem now is lombok Builder overrides my default constructor internally hence the value is never set.
How can I put any hook too make lombok #Builder use my default constructor?
Parent class:
#Getter
#Setter
public abstract class Record {
private String recordId;
}
Child class:
#Getter
#Setter
#Builder
#ToString
#AllArgsConstructor
public class SRecord extends Record {
private static final String RECORD_ID = "REC001";
private String street;
private String city;
public SRecord() {
setRecordId(RECORD_ID); //value of recordId being set
}
}
Lombok's #Builder simply does not use the default constructor. It passes its values to an all-args constructor so that this constructor can fill the new instance with these values. #Builder does not use setters or direct access to the fields to do so. So your default constructor is simply ignored by #Builder.
What you can do is write your own all-args constructor. In it, you set your value for recordId and assign the rest of the fields from the parameters.
I think you should create a constructor in your base class:
#Getter
#Setter
public abstract class Record {
private String recordId;
public Record(String recordId) {
this.recordId = recordId;
}
}
Then use it in the constructor of the inherited class:
#Getter
#Setter
#Builder
public class SRecord extends Record {
private static final String RECORD_ID = "REC001";
private String street;
private String city;
public SRecord(String street, String city) {
super(RECORD_ID);
this.street = street;
this.city = city;
}
}
P.S. If you want to use Lombok Builder with inheritance you can use this technique.

Categories