I have a DeviceController class as:
@RequestMapping(value = "/device", method = RequestMethod.POST,produces = "application/json")
public ResponseEntity<Device> createDevice(@RequestBody Device device) {
HttpHeaders headers = new HttpHeaders();
if (device == null) {
return new ResponseEntity<Device>(HttpStatus.BAD_REQUEST);
}
deviceRegisterService.createDevice(device)
headers.add("Device Created - ", String.valueOf(device.getDeviceId()));
return new ResponseEntity<Device>(device,headers,HttpStatus.CREATED);
}
I want to POST device info into Device Table(SQL database) and return a response i.e, a unique key with the Request data.
This is my repository class to query database:
@Repository
public class DeviceRepository {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int createDevice(Device device){
int count = jdbcTemplate.update("INSERT into Device_Table(device_name,device_type Values(?,?,?)",new Object{device.getdevice_name,device.getdevice_type,device.getDevice_id});
return count;
}
}
After hitting the server i am able to push the data but the unique ID which i am getting in response is completely different from the unique ID generated in Database.
I have a Device.java class that contains all getter/setter method and method to generate unique id. I am using UUID.randomUUID().toString() to generate Unique ID.
For example:
Request
{
device_name="asd"
device_type="xyz"
}
Response
{
device_name="asd"
device_type="xyz"
device_id="1223444455"
}
Database:
device_name : asd
device_type : xyz
device_id : "00999123"
I think that somehow my code is executing getDeviceID()
method two times. Can someone please help me rectify it.