I am writing an API that create some terminals. I want to make a post request from postman with a JSON body but it gives me error. I have searched about how to pass a object and tried the solutions. I don't know I am making post request with bad format or there is a problem in somewhere in my code. so, I would be very appreciated if anyone could help me.
I have a Terminal (terminal is somehow a device) class which is as follows:
@Entity
@Table(name = "Terminals")
public class Terminal {
@Id
private long terminalID;
@Column(name = "serialNumber")
private String serialNumber;
@ManyToOne(fetch = FetchType.LAZY)
private Model model;
@Column(name = "operatingSys")
private String operatingSys;
@Column(name = "brand")
private String brand;
@Column(name = "location")
private String location;
@Column(name = "version")
private String version;
public Terminal() {
}
public Terminal(long terminalID, String serialNumber, Model model,
String operatingSys, String brand, String location,
String version) {
this.terminalID = terminalID;
this.serialNumber = serialNumber;
this.model = model;
this.operatingSys = operatingSys;
this.brand = brand;
this.location = location;
this.version = version;
}
as it is clear from the code, I have a Model class that Terminal has a many-to-one relation with Model class. and also my model class has one-to-many relation with the terminal class. and the Model class is as follows:
@Entity
@Table(name = "model")
public class Model {
@Id
private String modelID;
@OneToMany(
mappedBy = "model",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Terminal> terminals = new ArrayList<>();
@Column(name = "newVersion")
private String newVersion;
@Column(name = "newVersionDlUrl")
private String newVersionDlUrl;
public Model() {
}
public Model(String modelID, String newVersion, String newVersionDlUrl) {
this.modelID = modelID;
this.newVersion = newVersion;
this.newVersionDlUrl = newVersionDlUrl;
}
public void addTerminal(Terminal terminal) {
terminals.add(terminal);
terminal.setModel(this);
}
public void removeTerminal(Terminal terminal) {
terminals.remove(terminal);
terminal.setModel(null);
}
also this is my API for creating a Terminal:
@PostMapping("/terminals")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Terminal> createTerminal(@RequestBody Terminal terminal) {
try {
Terminal _terminal = terminalRepository
.save(new Terminal(terminal.getTerminalID(),terminal.getSerialNumber(),
terminal.getModel(),terminal.getOperatingSys(),
terminal.getBrand(),terminal.getLocation(),terminal.getVersion()));
return new ResponseEntity<>(_terminal, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
also this is the way I make post request for creating terminals:
{
"terminalID": "456789",
"serialNumber": "a5487",
"model": {
"modelID": "galaxy"
},
"operatingSys": "android",
"brand": "samsung",
"location": "amsterdam",
"version": "1.1.2"
}
by this request I get Internal Server Error.
"message": "Type definition error: [simple type, class
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create
BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through
reference chain: com.mypackage.models.Terminal[\"model\"]-
>com.mypackage.models.Model$HibernateProxy$MJia9YtX[\"hibernateLazyInitializer\"])"
If anyone could help I would be very appreciated.