简体   繁体   English

如何在 Spring 引导 REST ZDB974238714CA8DE634A7CE1D083A14F 中格式化返回的 json?

[英]How to format returned json in Spring Boot REST API?

I created the following REST API with Spring Boot.我使用 Spring 引导创建了以下 REST API。

@RestController
public class PersonController {
    
    @Autowired
    private PersonRepository PersonRepository;
    
    @PostMapping(value="/Person", produces = "application/json")
    public ResponseEntity<Person> addPerson(@RequestBody Person Person){
        
        Person newPerson = PersonRepository.save(Person);       
        
        return new ResponseEntity<Person>(newPerson, HttpStatus.OK) ;
    }

}

In Postman I select:在 Postman I select 中:

POST > http://localhost:8080/person > json发布 > http://localhost:8080/person > json

Then in the body field I place the following json and hit send:然后在正文字段中,我放置以下 json 并点击发送:

{"id":"", "name":"john", "age":"40", "email":"test@test.com"}

This is the return I'm getting:这是我得到的回报:

{
    "name": "john",
    "age": 40,
    "email": "test@test.com",
    "_links": {
        "self": {
            "href": "http://localhost:8080/person/10"
        },
        "person": {
            "href": "http://localhost:8080/person/10"
        }
    }
}

But, I'd like to have the json return in this other format:但是,我想让 json 以这种其他格式返回:

{
    "id": 10,
    "name": "john",
    "age": 40,
    "email": "test@test.com"
}

How to do that?怎么做?

Looks like you are also using SpringDataRest which exposes _links etc. based on your entity.看起来您也在使用 SpringDataRest,它根据您的实体公开 _links 等。 I guess the controller from SpringDataRest is called in your request and not your custom endpoint.我猜您的请求中调用了来自 SpringDataRest 的 controller,而不是您的自定义端点。 Try to log something in your controller method to ensure that it's actually called.尝试在您的 controller 方法中记录一些内容,以确保它被实际调用。 If you don't need SpringDataRest at another part of your service you should remove the dependency.如果您的服务的另一部分不需要 SpringDataRest,则应删除依赖项。

I just found the issue.我刚刚发现了问题。

In the controller the code was:在 controller 中,代码为:

@PostMapping(value="/person"....

While in postman I was calling:在 postman 我打电话给:

http://localhost:8080/persons

(with the letter s at the end) (以字母 s 结尾)

As a result it never entered the controller method.结果它从未进入 controller 方法。

When I removed the s from the postman call it worked and returned the json just the way I wanted.当我从 postman 调用中删除 s 时,它可以正常工作并按照我想要的方式返回 json。

Now...现在...

The interesting part is that, when was calling with the letter s, it did save to the db and returned the json even the mapping being incorrect.有趣的是,当使用字母 s 调用时,它确实保存到数据库并返回 json,即使映射不正确。

So I decided to run on debug mode (with the incorrect mapping) and noticed it was not entering the method.所以我决定在调试模式下运行(使用不正确的映射)并注意到它没有进入方法。

That was weird.那很奇怪。 How could it correctly save and return the json?它如何正确保存并返回 json?

Then I decided to comment out all of the method and run the call on postman again.然后我决定注释掉所有方法并再次在 postman 上运行调用。

Once again it did save the json to the db and returned the json in the first format.它再次将 json 保存到数据库并以第一种格式返回 json。

So seems this is being done on behind the scenes somehow, surely not by the method I created.所以似乎这是在幕后以某种方式完成的,肯定不是通过我创建的方法。

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

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