3

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]]
5
  • What is this snippet doing @RequestBody int company_id? And what is the input that you are passing? Commented Nov 14, 2019 at 3:05
  • i want to create parameter with data type, how can i write this? Commented Nov 14, 2019 at 3:20
  • @RequestBody triggers an auto conversion and in your case it just fails. Change the int to KeyEntity or a mapper DTO and run this Commented Nov 14, 2019 at 3:23
  • Please share the request json you are using. Commented Nov 14, 2019 at 3:25
  • if I change the int to KeyEntity, where i write parameter? @aksappy Commented Nov 14, 2019 at 3:28

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.