1

Hi I am working on Rest API written in java and have come through the below doubt.

I have already written the code which takes post data and maps to pojo class and sends back the data in JSON form.

Correct Format:-

{
    "Name":"Rahul",
    "Address":"Mumbai",
    "Age": 27
}

Output:

{
    "Pkid":"1",
    "Name":"Rahul",
    "Address":"Mumbai",
    "Age": 27
}

But when I am passing empty value to one of the attribute I am getting 500 internal error can someone help me how to handle it and return the value in json format after handling it:

{
    "Name":,
    "Address":"Mumbai",
    "Age": 27
}

Output:

500 Internal error
Tarun Singh
  • 97
  • 2
  • 10
  • 1
    Can you post the relevant lines of your source code? The stack trace you see probably gives you a detailed answer of what is going on, so please post that too. – Christine Feb 17 '20 at 12:16
  • Does this help ? https://stackoverflow.com/questions/27502993/how-to-handle-internal-server-error-in-rest-api-using-java – Shubham Feb 17 '20 at 12:16
  • If you want to pass empty value don not use that attribute at all, JSON which you have used is invalid. Either use `{ "Name":"", "Address":"Mumbai" ,"Age": 27 }` or `{"Address":"Mumbai", "Age": 27 }` – Rahul Agrawal Feb 17 '20 at 12:20

3 Answers3

0

You can use Jackson library and set the below property while creating object.

val mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Add the below maven dependency :

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.7</version>
        </dependency>

You can see the answer here also.

EDIT 1: Deserialize your jsonObject as :

objectMapper.readValue(jsonString,POJO.class)
Prog_G
  • 1,539
  • 1
  • 8
  • 22
0

first of all, use a json validator. it seems that you have a missing comma.

https://jsonformatter.curiousconcept.com/

And also it is helpful to share corresponding code, and retrieved exception etc.

Aytug
  • 59
  • 6
0

Understanding: you are trying to store an object and return the object details along with the primary key id generated Following are two approaches for this:

1. Custom de-serializer with Jackson: I would prefer this method because it provides you with additional security. In the current scenario, you are exposed to cross-site scripting attacks Using a custom de-serializer along with Jsoup api or Antisamy to filter the data would help you prevent XSS and also include null values.

You need to add your custom de-serializer to your pojo using:

    @JsonDeserialize(using = CustomDeserializer.class)
    public class User {
       private String id;

       private String name;
      // standard getters and setters
    }

2. Using Jackson annotation:

public class User {

    @JsonInclude(Include.NON_NULL)
    private String id;

    private String name;

    // standard getters and setters
}
Dharmvir Tiwari
  • 886
  • 3
  • 12
  • 25