I'm new in spring boot
How can i do to get all properties
coming from client(Postman) json in spring
boot?
For exemple i have classe
Class A{
String a;
}
Classe B extens A{
String b;
}
Class C {
long id;
List<B> c=new arraylist<>();
}
In my controller
#Postmaping()
test(#Requestbody C c){
Sysout(c);// I want get all properties a and b
}
i only got the b properties not a
My json
{ "id":1, "c":[{"a":"a","b":"b"}] }
In your example you have Class B extending from Class A. Therefore B is more specific. If your RequestBody contains string a and string b
{
"a":"valueA",
"b":"valueB"
}
you have to change your request Body to receive Class B.
#PostMapping("/some/endpoint)
public void test(#Requestbody B b){
System.out.println(b); // contains valueA and valueB
}
After a hard debugging match, I just realized that I was wrong #Override the toString() method of the child class.
It was simply necessary to add super.toString() to child class
#Override
public String toString() {
return super.toString() +
"b ='" + b+ '\'' +
'}';
}
Related
Suppose I have a complex entity like the follow:
class A {
private String a;
private String b;
private String c;
/**
* so on */
private B bb;
private C cc;
}
and the respective DTO:
class ADTO {
private String a;
private String b;
private String c;
/**
* so on */
private BDTO bb;
private CDTO cc;
}
Now suppose C entity (and CDTO) has many variables and I want map is with just his ID field. For example
#Mapper(componentModel = "spring")
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
CDTO cToDto(C c); // for this I want to map just the id!!
C cFromDto(CDTO cDto); // even for this
}
How can I do?? I wouldn't write 50 times #Mapper(properties = "someField", ignore = true), is there another method??
Thank you
You can create a separate mapper for C class and add in AMapper class using uses field of #Mapper
#Mapper(componentModel = "spring", uses={"CMapper.class"})
public Interface AMapper {
BDTO bToDto(B b);
B bFromDto(BDTO bDto);
// remove below
// CDTO cToDto(C c);
// C cFromDto(CDTO cDto);
}
public CMapper{
public C fromDTO(CDTO) {
// add mapping for ID only
}
public CDTO toDTO(C) {
// add mapping for ID only
}
}
My example is,
#Entity
#Getter
#Setter
public class MyEntity {
private String A;
private String B;
#JsonIgnore #Column(unique = true)
private String C;
public String getC() {
return this.A + "_" + this.B;
}
public void setC(String C) {
this.C = this.A + "_" + this.B;
}
}
i want to combine at C by A + B strings.
because it's my unique key for something useful.
(ps,
In fact, I should be the only A + B combination. If you have any other suggestion, I would like to recommend it.)
my POST request example is,
"A" : "value A",
"B" : "value B"
without C like above.
so i tried change at getter and setter to return A+B;
but it didn't work.(C is null)
what should i do???
Thanks to #M.Deinum I solved it by using
#Table(uniqueConstraints=#UniqueConstraint(columnNames = {"A", "B"}))
I have a doubt to use autowire annotation in different scenarios...
public Class A {
#Autowired
public B b;
public void print() {
System.out.println("the value is " + b.getValue());
}
}
Example 2:
public Class A {
public B b;
#Autowired
Public A(B b){}
public void print() {
System.out.println("the value is " + b.getValue());
}
}
The first method to autowire objects is called field-injection and the second one is called constructor-injection. There are many posts on SO about these two topics! E.g. What exactly is Field Injection and how to avoid it?
This should answer all of your questions.
I have a
Class A {
int a;
String name;
}
Class B {
int id;
A objectA;
}
When I return the data of Object A from RestController it returns the null for Class B object even if it has the data inside.
We are using JPA Entities to get the database rows and then when we transfer that to the external, we want to use disconnected object (DTO) which are simple beans annotated with JAX-B.
We use a mapper and its code looks like this:
public BillDTO map(BillEntity source, BillDTO target) {
BeanUtils.copyProperties(source, target);
return target;
}
But when the code is running we get an error like this:
java.lang.IllegalArgumentException: argument type mismatch
Note this is the Spring implementation of the BeanUtils:
import org.springframework.beans.BeanUtils
And the naming of the properties are identical (with their getter/setter).
Anybody knows why the error happens?
And how to use a fast way instead just copying properties one by one?
This example working well. Here String property is copied to enum property:
Entity:
public class A {
private String valueFrom;
public String getValue() {
return valueFrom;
}
public void setValue(String value) {
this.valueFrom = value;
}
}
DTO (En is enumeration):
public class B {
private En valueTo;
public void setValue(String def) {
this.valueTo = En.valueOf(def);
}
public void setEnumValue(En enumVal) {
this.valueTo = enumVal;
}
}
As for your GitHub example, problem in class B in getter should be:
public String getValue()
Example:
public String getValue() {
return value.toString();
}