Ker Laeda
Ker Laeda

Reputation: 95

Spring. How to return ObjectId field as String in response of Rest controller?

I have Rest controller on Spring.

Controller:

@RestController
@RequestMapping("/users")
public class Controller {

    private UserService userService;

    @Autowired
    public Controller(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/all")
    public List<User> fethAllUsers() {
        return userService.fetchAllUsers();
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable ObjectId id) {
        return userService.fetchUser(id);
    }

    @PostMapping("/")
    public User createUser(@RequestParam String name, @RequestParam String address) {
        return userService.createUser(name, address);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable ObjectId id) {
        userService.deleteUser(id);
    }
}

Service:

@Service
public class UserService {

    private UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public List<User> fetchAllUsers() {
        return userRepository.findAll();
    }

    public User fetchUser(ObjectId id) {
        Optional<User> optionalUser = userRepository.findById(id);
        return optionalUser.orElseGet(optionalUser::get);
    }

    public User createUser(String name, String address) {
        User user = new User();
        user.setId(ObjectId.get());
        user.setName(name);
        user.setAddress(address);
        return userRepository.save(user);
    }

    public void deleteUser(ObjectId id) {
        userRepository.deleteById(id);
    }
}

Repository:

@Repository
public interface UserRepository extends MongoRepository<User, ObjectId> {
}

Entity:

import lombok.Data;
import org.bson.types.ObjectId;

@Data
public class User {
    private ObjectId id;
    private String name;
    private String address;
}

Currently when I use method @GetMapping in Postman (or browser) in response JSON object in get:

[
    {
        "id": {
            "timestamp": 1569940881,
            "machineIdentifier": 9478066,
            "processIdentifier": 10308,
            "counter": 14051396,
            "time": 1569940881000,
            "date": "2019-10-01T14:41:21.000+0000",
            "timeSecond": 1569940881
        },
        "name": "testName",
        "address": "testAddress"
    }
]

And in response I need field "id" in String format and looks like:

[
    {
        "id": "5d936591909fb22844d66844",
        "name": "testName",
        "address": "testAddress"
    }
]

How can I return in response JSON object fielt "id" as ObjectId in String format?

Upvotes: 0

Views: 3096

Answers (1)

Kevin Montalvo
Kevin Montalvo

Reputation: 979

I have a solution very simple:

@JsonSerialize(using= ToStringSerializer.class)
private ObjectId id;
...

put that on the attribute that is Object Id, and the ObjectId gets and inserts like string

here my answer

Upvotes: 6

Related Questions