BAEL-6032 - @JsonIgnore vs @Transient (#13254)
* BAEL-6032 - @JsonIgnore vs @Transient * Rename test methods
This commit is contained in:
parent
dc5641200e
commit
02b56bf783
|
@ -25,6 +25,27 @@
|
||||||
<version>${rest-assured.version}</version>
|
<version>${rest-assured.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>${h2.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -38,7 +59,9 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<h2.version>2.1.214</h2.version>
|
||||||
<rest-assured.version>3.1.1</rest-assured.version>
|
<rest-assured.version>3.1.1</rest-assured.version>
|
||||||
|
<spring-boot.version>2.5.0</spring-boot.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.jackson.jsonignorevstransient;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.jackson.jsonignorevstransient;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
class Person implements Serializable {
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
|
private final Long id;
|
||||||
|
|
||||||
|
private final String firstName;
|
||||||
|
|
||||||
|
private final String lastName;
|
||||||
|
|
||||||
|
public Person(Long id, String firstName, String lastName) {
|
||||||
|
this.id = id;
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package com.baeldung.jackson.jsonignorevstransient;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
import javax.persistence.Transient;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "Users")
|
||||||
|
class User implements Serializable {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
private String repeatedPassword;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(Long id, String username, String password, String repeatedPassword) {
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.repeatedPassword = repeatedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRepeatedPassword() {
|
||||||
|
return repeatedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRepeatedPassword(String repeatedPassword) {
|
||||||
|
this.repeatedPassword = repeatedPassword;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.jackson.jsonignorevstransient;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.jackson.jsonignorevstransient;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
|
||||||
|
class PersonUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenPerson_whenSerializing_thenIdFieldIgnored() throws JsonProcessingException {
|
||||||
|
|
||||||
|
Person person = new Person(1L, "My First Name", "My Last Name");
|
||||||
|
String result = new ObjectMapper().writeValueAsString(person);
|
||||||
|
|
||||||
|
assertThat(result, containsString("firstName"));
|
||||||
|
assertThat(result, containsString("lastName"));
|
||||||
|
assertThat(result, not(containsString("id")));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.jackson.jsonignorevstransient;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class UserUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserRepository userRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenUser_whenSave_thenSkipTransientFields() {
|
||||||
|
User user = new User(1L, "user", "newPassword123", "newPassword123");
|
||||||
|
User savedUser = userRepository.save(user);
|
||||||
|
|
||||||
|
assertNotNull(savedUser);
|
||||||
|
assertNotNull(savedUser.getPassword());
|
||||||
|
assertNull(savedUser.getRepeatedPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenUser_whenSerializing_thenTransientFieldNotIgnored() throws JsonProcessingException {
|
||||||
|
User user = new User(1L, "user", "newPassword123", "newPassword123");
|
||||||
|
String result = new ObjectMapper().writeValueAsString(user);
|
||||||
|
|
||||||
|
assertThat(result, containsString("user"));
|
||||||
|
assertThat(result, containsString("repeatedPassword"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
hibernate.hbm2ddl.auto=create-drop
|
||||||
|
|
||||||
|
spring.datasource.jdbcUrl=jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=sa
|
Loading…
Reference in New Issue