0

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.

Faisal Jaffri
  • 37
  • 1
  • 12
  • this is a duplicate question. Please refer to this link [https://stackoverflow.com/questions/1915166/how-to-get-the-insert-id-in-jdbc] – webmite May 30 '18 at 12:10
  • Possible duplicate of [How to get the insert ID in JDBC?](https://stackoverflow.com/questions/1915166/how-to-get-the-insert-id-in-jdbc) – seenukarthi May 30 '18 at 12:12
  • @webmite I dont think i have an issue in SQL query. I think i am facing trouble in Controller class. – Faisal Jaffri May 30 '18 at 12:13
  • @KarthikeyanVaithilingam I am getting unique ID in response(Using Postman), but that unique ID is different from the Unique ID generated in DB . – Faisal Jaffri May 30 '18 at 12:17
  • The generated DB id is not propagated back to the Device object. I guess the id in the response originates from the POST params were the Device is send to the controller. – brass monkey May 30 '18 at 12:20
  • Yes @LazerBass it could possibly be a reason. But cant figure out how to rectify it. – Faisal Jaffri May 30 '18 at 12:26
  • You need to add the `Statement.RETURN_GENERATED_KEYS` clause to your query update call. The ID is generated by the database, not by you. Please read the answer to the question I referred you to. – webmite May 30 '18 at 12:26
  • @webmite I am using "UUID.randomUUID().toString()" to generate Unique ID. I have made a function in my MODEL class(Device.java), and i am calling this function(Get_DeviceID() ) while i am querying the database. – Faisal Jaffri May 30 '18 at 12:34
  • Why do you want to return a singe ID? As you are using HTTP as your communication channel, a response for the processed request should, accroding to [RFC 7231](https://tools.ietf.org/html/rfc7231#section-4.3.3), return a 201 response containing the current resource state in the format processable by the client AND a location header pointing to the endpoint the state is optainable by subsequent requests. Anything other screams for interoperability issues ... – Roman Vottner May 30 '18 at 12:41
  • @RomanVottner The main reason is that, suppose a user entered value of Device name,device type, etc.... than it should get updated in the database along with a unique ID. So that from the next time it is easy to search for things with respect to that ID. That is the reason i need ID as response for future reference. – Faisal Jaffri May 30 '18 at 12:45
  • This requires clients to have explicite knowledge of your API and lead to failures of those clients if you ever going to change anything. According to one of the post's tags you are refering to a REST solution. While REST is not a standard/protocol, its intention is to design systems that do not tightly couple clients to APIs. This allows APIs to evolve without having to fear breaking clients. If you aren't interested in such benefits, please remove the [tag:rest] from your question – Roman Vottner May 30 '18 at 12:52
  • post your Entiity class – sam May 30 '18 at 14:28
  • @SAM Please check it out – Faisal Jaffri May 31 '18 at 08:05
  • its because you are calling a `@Repository` annotated class directly from `@Controller` class. Introduce a `@Service` annotated class in between repository and controller, then it will get solved. – sam May 31 '18 at 13:55

0 Answers0