Reputation:
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]]
Upvotes: 3
Views: 9769
Reputation: 6412
If your JSON comes from Postman, you get this error when you try to send in a variable without quotes:
{ "key": {{myIntVariable}} }
instead of
{ "key": "{{myIntVariable}}" }
Even numbers need to be wrapped with quotes this way.
Upvotes: 0
Reputation: 1274
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.
Upvotes: 1
Reputation: 266
You should be using
String simpanKey(@RequestBody RequestPOJO request) {
Instead of
String simpanKey(@RequestBody int company_id) {
And then using request extract all the fields coming from POST HTTP body, where RequestPOJO can look like
class RequestPOJO {
int company_id;
}
Upvotes: 3