Stop nested resources being mapped to '/' - Spring Boot

Viewed 17

I'm currently learning about Spring Boot and am undertaking a project where users can make posts, view those posts, etc.

A user's post(s) can be viewed via http://localhost:8080/users/{user_id}/posts and http://localhost:8080/users/{user_id}/posts/{post_id}

As a result I have the following UserPostController

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

    @Autowired
    private UserPostService postService;

    @GetMapping("/{user_id}/posts")
    public List<Post> retrieveUserPosts(@PathVariable int user_id) {
        return postService.retrieveUserPostList(user_id);
    }

    @GetMapping("/{user_id}/posts/{post_id}")
    public EntityModel<Post> retrieveUserPost(@PathVariable int user_id, @PathVariable int post_id) {
        return postService.retrieveUserPost(user_id, post_id);
    }

    @PostMapping("/{user_id}/posts")
    public ResponseEntity<Object> createUserPost(@PathVariable int user_id, @Valid @RequestBody Post post) {    
        return postService.saveUserPost(user_id, post);       
    }
}

Every request to the links work correctly. For example a GET request to http://localhost:8080/users/1/posts returns [{"id":1,"description":"This is a post"},{"id":2,"description":"This is another post"}], which is the expected action.

However, for some reason I am able to visit http://localhost:8080/posts which then returns a list of all posts:

{
  "_embedded" : {
    "posts" : [ {
      "description" : "This is a post",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "post" : {
          "href" : "http://localhost:8080/posts/1"
        },
        "user" : {
          "href" : "http://localhost:8080/posts/1/user"
        }
      }
    }, {
      "description" : "This another post",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "post" : {
          "href" : "http://localhost:8080/posts/2"
        },
        "user" : {
          "href" : "http://localhost:8080/posts/2/user"
        }
      }
    }, {
      "description" : "This is yet another post",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/posts/3"
        },
        "post" : {
          "href" : "http://localhost:8080/posts/3"
        },
        "user" : {
          "href" : "http://localhost:8080/posts/3/user"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/posts"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/posts"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 3,
    "totalPages" : 1,
    "number" : 0
  }
}

Through HATEOAS I am able to also see available links of the format http://localhost:8080/posts/{user_id}/user which I have also not created methods for, but they still exist.

Is there a reason why these unwanted routes exist? If so how do I change this? Thank you :)

0 Answers
Related