1

I am new to development of REST webservices. Currently, we are using Spring Boot, Spring JPA for the development of services. I am facing difficulty in returning selective columns in the REST response. My problem is as below:

I have a user table with 10 rows with below columns

  1. Id
  2. Name
  3. Age
  4. City
  5. Mobile number

My user Entity object is:

@Entity
@Table(name="user")
public class User
{

@Id
@GeneratedValue(generator="ID", strategy=GenerationType.SEQUENCE)
private long id;

@Column(name="Name")
private string name;

@Column(name="age")
private int age;

@Column(name="city")
private string city;

@Column(name="mobile number")
private string mobileNumber;
.
.
.
getter and setters
.
.
.}

I have below two requirements to develop:

  1. To return all the rows with all the columns
  2. To return all the rows with only three columns (Id, Name and mobile number)

For the first requirement, I was able to get it working by using the findAll() of JPA

For the second requirement, I tried using a custom query as below:

@Query("select New com.org.user(u.id, u.name, u.mobileNumber) from user u")
public List<user> getList()

With this, my Json response was looking as below with the other two fields (city and age) having default values but not the actual values in table.

[
{
"id": 1,
"name":"Rocky"
"age":0,
"city":null,
"mobileNumber": "9080980909"
},
{
"id": 2,
"name":"Stella"
"age":0,
"city":null,
"mobileNumber": "8909012239"
}
.
.
.
.
{
"id": 10,
"name":"Una"
"age":0,
"city":null,
"mobileNumber": "78976887989"
}
]

But I need the response to be like below to contain only three fields

[
{
"id": 1,
"name":"Rocky",
"mobileNumber": "9080980909"
},
{
"id": 2,
"name":"Stella",
"mobileNumber": "8909012239"
},
.
.
.
{
"id": 10,
"name":"Una",
"mobileNumber": "78976887989"
}
]

Can someone please help as to how can i get the response as above?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Harish
  • 121
  • 1
  • 1
  • 6
  • JsonView: https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring – Alan Hay Aug 09 '16 at 12:21
  • Hi Alan, is there any other way of doing this instead of creating POJOs for every custom response that we need? – Harish Aug 09 '16 at 13:13
  • Yes,use JsonView as advised or configure your serializer to ignore nulls e.g. for Jackson: http://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null. For non-nullable primitives boolean, integer etc, use the nullable wrapper types Boolean, Integer etc in your Pojo. – Alan Hay Aug 09 '16 at 13:15

1 Answers1

0

Use @JsonInclude(Include.NON_NULL) annotation on your entity. It will exclude all null fields on your json response. But then, you have to change "age" from int to Integer data type for it to nullable.

@Entity
@Table(name="user")
@JsonInclude(Include.NON_NULL)
public class User
{

@Id
@GeneratedValue(generator="ID", strategy=GenerationType.SEQUENCE)
private long id;

@Column(name="Name")
private String name;

@Column(name="age")
private Integer age;

@Column(name="city")
private String city;

@Column(name="mobile number")
private String mobileNumber;
.
.
.
getter and setters
.
.
.}
Leo
  • 222
  • 3
  • 12
  • what if i have a field which is a boolean does @JsonInclude(Include.NON_NULL) work? – Harish Aug 09 '16 at 13:10
  • change the data type to Boolean, don't use primitive data types if you want them nullable. – Leo Aug 09 '16 at 13:38