Home > Back-end >  Spring-boot mongodb - how to convert MongoRepository save response to custom class and return as JSO
Spring-boot mongodb - how to convert MongoRepository save response to custom class and return as JSO

Time:10-13

I'm trying to learn spring-boot with mongodb. I've followed online articles and tutorials and the sample application is working fine for returning string as API response. But when I'm trying to return response as JSON using ResponseEntity I'm getting an error.

Here's the code:

src/customerapi/model/Customer.java

package com.customerapi.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString

@Document(collation = "customer")
public class Customer {
    @Id
    private String custRefId;
    
    private String title;

    private String firstName;
    
    private String middleName;

    private String lastName;
    
    private String businessName;

    private String email;

    private String phone;

    private String note;
    
    private String dateOfBirth;

    private String sex;
    
    
}

src/customerapi/dto/CustomerResponse.java

package com.customerapi.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@JsonInclude(Include.NON_NULL)
public class CustomerResponse {
    @JsonProperty("custRefId")
    private String custRefId;
    
}

src/customerapi/repository/CustomerRepository.java

package com.customerapi.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.customerapi.dto.CustomerResponse;
import com.customerapi.model.Customer;

public interface CustomerRepository extends MongoRepository<Customer, String> {
}

src/customerapi/resource/CustomerController.java

package com.customerapi.resource;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.customerapi.repository.CustomerRepository;
import com.customerapi.model.Customer;
import com.customerapi.dto.CustomerResponse;

@RestController
public class CustomerController {
    
    @Autowired
    private CustomerRepository customerRepository;
    
    @PostMapping(value="/addCustomer", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<CustomerResponse> saveCustomer(@RequestBody Customer customer) {
        
        try {
            return new ResponseEntity<CustomerResponse>(new customerRepository.save(customer), HttpStatus.CREATED);
        }finally {
            System.out.println("Issue with saving data in DB");
        }
    }
}

Returning a string from saveCustomer() works fine. But when I'm trying to return ResponseEntity<CustomerResponse> and hit the endpoint in Postman I'm getting this error.

postman error

I searched online and tried multiple things from stackoverflow answers but nothing seems to be working for me.

**How do I convert MongoRepository's save response to custom class and return as JSON using ResponseEntity? **

CodePudding user response:

Here new CustomerResponse(customerRepository.save(customer)). Inside CustomerResponse there is no constructor which accepts Customer as a parameter. You can create one. So your CustomerResponse would look like this.

import com.customerapi.model.Customer;

@Getter
@Setter
@ToString
@JsonInclude(Include.NON_NULL)
public class CustomerResponse {
    @JsonProperty("custRefId")
    private String custRefId;
    
    public CustomerResponse(Customer customer){
        this.custRefId = customer.getCustRefId();
    }
}
  • Related