简体   繁体   English

Spring。 如何将 ObjectId 字段作为字符串返回以响应 Rest controller?

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

I have Rest controller on Spring.我在 Spring 上有 Rest controller。

Controller: 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:目前,当我在 Postman (或浏览器)中使用方法 @GetMapping 以响应 JSON object 时:

[
    {
        "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”,如下所示:

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

How can I return in response JSON object fielt "id" as ObjectId in String format?如何在响应中返回 JSON object 字段“id”作为字符串格式的 ObjectId?

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把它放在 Object Id 的属性上,ObjectId 像字符串一样获取和插入

here my answer这是我的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM