title: Java JSON - Override @JsonIgnore for tests
body: <p>I have project in Spring Boot. I have User model, what have Profile model in relation <code>OneToOne</code>:</p>
  
  <p>User: (Simplified)</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class User extends AbstractEntity {
  
      @Id
      @GeneratedValue
      private Integer id;
  
      @NotEmpty
      @Basic(optional = false)
      @Column(nullable = false, unique = true)
      private String username;
  
      @Valid
      @JsonIgnore
      @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
      private Profile profile;
  
      @JsonIgnore
      public Profile getProfile() {
          return profile;
      }
  
      @JsonProperty
      public void setProfile(Profile profile) {
          this.profile = profile;
      }
  }
  </code></pre>
  
  <p>Profile: (Simplified)</p>
  
  <pre><code>@Entity
  @Table(name = "profiles")
  public class Profile extends AbstractEntity {
  
      @Id
      @GeneratedValue
      private Integer id;
  
      @NotEmpty
      @Basic(optional = false)
      @Column(nullable = false)
      private String name;
  
      @NotEmpty
      @Basic(optional = false)
      @Column(nullable = false)
      private String surname;
  
      // Getters, setters, etc
  }
  </code></pre>
  
  <p>My test:</p>
  
  <pre><code> @Test
      public void createUserAndProfileReturnsCreatedStatus() throws Exception {
          final User user = Generator.generateUser();
          user.setProfile(Generator.generateProfile());
          MvcResult mvcResult = this.mockMvc.perform(
                  post("/users")
                          .contentType(MediaType.APPLICATION_JSON)
                          .content(toJson(user)))
                  .andExpect(status().isCreated())
                  .andReturn();
      }
  </code></pre>
  
  <p>Problem is, when i do <code>user.setProfile()</code>, Profile is set into User but when i call <code>toJson(user)</code> its automatically ignored because of my annotations in model.</p>
  
  <p>How to disable those annotations just for purpose of testing? Is it possible?
  I dont want to remove @JsonIgnore annotations from model, because they are there to not expose Profile when I <code>READ</code> user by <code>GET /users/&lt;id&gt;</code>.</p>