1

I created the following REST API with Spring Boot.

@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:

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

Then in the body field I place the following json and hit send:

{"id":"", "name":"john", "age":"40", "email":"[email protected]"}

This is the return I'm getting:

{
    "name": "john",
    "age": 40,
    "email": "[email protected]",
    "_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:

{
    "id": 10,
    "name": "john",
    "age": 40,
    "email": "[email protected]"
}

How to do that?

4
  • 2
    Person vs. person? Commented Feb 12, 2021 at 17:49
  • 2
    is _links a part of your person entity? I tried to reproduce this but my response doesn't include the _links part that yours does. Commented Feb 12, 2021 at 17:56
  • pls post your Person class Commented Feb 12, 2021 at 18:10
  • I found the issue. Just posted the answer below. Thanks everyone. Commented Feb 12, 2021 at 18:13

2 Answers 2

1

Looks like you are also using SpringDataRest which exposes _links etc. based on your entity. I guess the controller from SpringDataRest is called in your request and not your custom endpoint. Try to log something in your controller method to ensure that it's actually called. If you don't need SpringDataRest at another part of your service you should remove the dependency.

Sign up to request clarification or add additional context in comments.

7 Comments

Right. I have this dependency <artifactId>spring-boot-starter-data-rest</artifactId>. I found the issue. It was not entering my custom controller method indeed. I just posted the answer with the details. Thank you.
I think you got confused with SpringDataRest. It's automatically exposing rest controllers for all your entities (including CRUD operations). as you requested the service with /person you actually called the automatically generated controller and not your method. The request with /Person called your method properly. You should remove this dependency if you don't want auto-generated controllers for your entities.
the dependency you mention is spring-boot-starter-data-rest? I can't find SpringDataRest anywhere in the project.
Yes exactly! you might have enabled this dependency at your repository interfaces with @RepositoryRestResource. When you remove the dependency you should also remove these annotations to not get a compile error
cool. I checked your answer as accepted. Thank you very much.
|
0

I just found the issue.

In the controller the code was:

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

While in postman I was calling:

http://localhost:8080/persons

(with the letter s at the end)

As a result it never entered the controller method.

When I removed the s from the postman call it worked and returned the json just the way I wanted.

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.

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?

Then I decided to comment out all of the method and run the call on postman again.

Once again it did save the json to the db and returned the json in the first format.

So seems this is being done on behind the scenes somehow, surely not by the method I created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.