0

I have a Spring controller with the below mappings

@RestController
public class UserController {

    public UserService service;

    public UserController(UserService service) {
        this.service = service;
    }

    @PostMapping("/users/create")
    ResponseEntity<User> create (@RequestBody User user){
        return new ResponseEntity<>(this.service.create(user), HttpStatus.CREATED);
    }

    @GetMapping("/users/{id}")
    ResponseEntity<User> getById(@PathVariable Long id) {
        return ResponseEntity.ok(this.service.findById(id));
    }

    @GetMapping("/users/all")
    List<User> getAll() {
        return this.service.findAll();
    }

    @PutMapping("/users/{id}")
    User updateUser(@RequestBody User newUser, @PathVariable Long id) {
        return this.service.updateUser(newUser, id);
    }

    @DeleteMapping("/users/{id}")
    void deleteUser(@PathVariable Long id) {

        this.service.deleteById(id);
    }

}

and when I send a request to http://localhost:8080/users/all I get a list of all users as expected. But I noticed that if I send a GET request to just http://localhost:8080/users it returns a list with all the data as shown below

{
    "_embedded": {
        "users": [
            {
                "admin": false,
                "name": "Name",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/users/1"
                    },
                    "user": {
                        "href": "http://localhost:8080/users/1"
                    },
                    "tickets": {
                        "href": "http://localhost:8080/users/1/tickets"
                    }
                }
            },
            {
                "admin": false,
                "name": "Example",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/users/2"
                    },
                    "user": {
                        "href": "http://localhost:8080/users/2"
                    },
                    "tickets": {
                        "href": "http://localhost:8080/users/2/tickets"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/users"
        },
        "profile": {
            "href": "http://localhost:8080/profile/users"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 4,
        "totalPages": 1,
        "number": 0
    }
}

How come spring is able to return this data even though it has no explicit mapping and how can I disable it. This is just for a personal training exercise so security is not vital but I would like to know how to avoid this as I could have accidentally exposed all the data had this been real.

Any advice would be appreciated, thanks.

Update: As it has been suggested that the GET (/users/{id}) mapping was using a null value I commented it out and tried to call it again. Providing it with a number returned a 405 as the service no longer exists but I tried it with http://localhost:8080/users/ again but it still returned all the data shown above

A_Carolan
  • 23
  • 6
  • @Thomas Method parameters annotated with `@PathVariable` are required by default. If it is optional, the `required = false` need to be used – Gautham M Mar 24 '21 at 13:26
  • 1
    Wild guess (well not really) you have `spring-boot-starter-data-rest` as a dependency. If so remove it. – M. Deinum Mar 24 '21 at 14:19

2 Answers2

2

It looks like you are using HATEOAS format in Spring Data Rest https://spring.io/projects/spring-data-rest.

You can try to disable it by setting paramerer: spring.data.rest.defaultMediaType=application/json

Mihail
  • 323
  • 2
  • 7
  • I've just tried adding it to the properties and it is still returning the all the data but in a slightly different format – A_Carolan Mar 24 '21 at 13:44
0

I've managed to find the the solution, adding :

@SpringBootApplication(exclude = RepositoryRestMvcAutoConfiguration.class)

to the main method will disable the HATEOAS functions

I found the solution from this post: Spring Data Rest Without HATEOAS

A_Carolan
  • 23
  • 6