How to Fix This ERROR "JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT

user8108863 :

I want to build simple REST service API save to database postgresql using spring boot java. when I want to insert data to database but show errors, How can I fix it this problem? JSON parse error: Cannot deserialize instance of int out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of int out of START_OBJECT token, please Help me...

Here is the Rest Service Code

@RestController
@RequestMapping("/")
public class GenerateKeyController {


@Autowired
private GenerateKeyRepository gkrepo;

//getAll
@RequestMapping("/getAll")
List<KeyEntity> getAll(){
    return gkrepo.getAll();
}

//insert key
@RequestMapping(path ="/savekey", method = RequestMethod.POST)
    String simpanKey(@RequestBody int company_id) {
    String encKey = null;


    if (gkrepo.getOne(company_id) != null) {
     return "error, company sudah memiliki key";    
    }


    //terus save
    KeyEntity objKeyEntity;

    objKeyEntity = new KeyEntity();
    objKeyEntity.setCompanyid(company_id);
    objKeyEntity.setKeyencrypted(encKey);
    objKeyEntity.setCreationdate(new Date());

    gkrepo.save(objKeyEntity);

        return encKey;

     }
   }

This is My Entity

@Entity
@Table(name= "tb_key")
public class KeyEntity {

@Id
private Integer companyid;
private Date creationdate;
private String keyencrypted;

public Integer getCompanyid() {
    return companyid;
}
public void setCompanyid(Integer companyid) {
    this.companyid = companyid;
}
public Date getCreationdate() {
    return creationdate;
}
public void setCreationdate(Date creationdate) {
    this.creationdate = creationdate;
}
public String getKeyencrypted() {
    return keyencrypted;
}
public void setKeyencrypted(String keyencrypted) {
    this.keyencrypted = keyencrypted;
}

}

This is my Error Messages:

   2019-11-14 09:34:42.402  WARN 6932 --- [io-20000-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 4]]
ASK :

If you don't want to provide complete request body in the request, instead you can do it by providing the company_id as 'PATH VARIABLE' OR 'REQUEST PARAM'. e.g.

1.Using PathVariable

@RequestMapping(path ="/savekey/{company_id}", method = RequestMethod.POST)
String simpanKey(@PathVariable int company_id) {

// your predefined logic
// now it will be provided in the url without any request body

}

In this case, your url will be :
**http://111.111.1.111:0000/savekey/1**

2.Using RequestParam :

@RequestMapping(path ="/savekey", method = RequestMethod.POST)
String simpanKey(@RequestParam int company_id) {

 // your predefined logic
 // now it will be provided in the url without any request body

}

In this case, your url will be :
**http://111.111.1.111:0000/savekey?company_id=1**

Hope, you can consider or try these as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related