1

Here is my User class :

@Data
@Entity
public class User {

    @Id @GeneratedValue Long userID;
    String eMail;
    String phoneNumber;
    String displayName;
    //File displayPicture;
    String firstName;
    String middleName;
    String lastName;
    ArrayList<ClassRoom>adminOf=new ArrayList<>();
    ArrayList<ClassRoom>memberOf=new ArrayList<>();
    @NotNull
    int rating;
    boolean isTeacher;
    ArrayList<Assignment>assignmentsToSubmit=new ArrayList<>();
    ArrayList<Assignment>assignmentsSubmitted=new ArrayList<>();

    @OneToOne(fetch = FetchType.LAZY,targetEntity = LoginCredential.class)
    @JoinColumn(name = "userID",referencedColumnName = "userID")
    private LoginCredential loginCredential;

    User() {
    }
}

And here is the controller, UserController class :

@RestController
class UserController {

    private final UserRepository repository;
    UserController(UserRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/user/{id}")
    User one(@PathVariable Long id) {
        return repository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
    }
}

I accessed http://localhost:8080/user/1 through my browser. Here it is the output :

spring-h2-jpa get mapping

Here loginCredential is also showing up, but I want to return everything except it.

How this can be done, without another class?

5
  • a common practice is to not return your entitiers straight to the client, but instead have specific response objects so that the database has its API and the client gets exposed another API. Commented Aug 31, 2019 at 21:53
  • Can you please explain a little ? Commented Sep 1, 2019 at 1:33
  • You use @Entity annotated classes when you call the database. Then you move over everything from that class to another class and this object you return to the browser (client). This way you can add stuff to database without affecting the browser object, or you can add things to the browser object without affecting the database. Commented Sep 1, 2019 at 2:07
  • Concept is clear I guess, an example or reference would be very helpful Commented Sep 1, 2019 at 2:16
  • 1
    posted an answer to demo the concept Commented Sep 1, 2019 at 2:22

2 Answers 2

3

Add @JsonIgnore to the field definition. Alternatively, you can use @JsonIgnoreProperties on the class level.

I notice you tried giving the field private access modifier. This won’t hide the field to the JSON serializer, as your class is annotated with Lombok’s @Data which, when used without @Getter/@Setter with overriden access, sets access to generated methods to public.

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

Comments

1

Or you can have another object that you return to the browser

@Builder
@Data
public class UserResponse {

    private String eMail;
    private String phoneNumber;
    private String displayName;

    // omitted the rest because im lazy

}


@RestController
public class UserController {

    private final UserRepository repository;

    @Autowire
    public UserController(UserRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/user/{id}")
    public UserResponse one(@PathVariable Long id) {
        final Optional<UserEntity> user = repository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
        return user.map(userEntity -> {
            return UserResponse.builder()
                .eMail(userEntity.getEMail())
                .phoneNumber(userEntity.getphoneNumber())

                // omitted the rest because im lazy

                .build();
        })

    }
}

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.