title: SPRING REST controller - return image AND json values
body: <p>I have built a rest web service, using SPRING and Hibernate.
  I have 2 entities : Image and user, linked with a oneToOne annotation.</p>
  
  <p>When I try to return the user details AND the image corresponding to this user, I get this error : 
  "org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation"</p>
  
  <p>When I do it separately It works fine, but when I do it in one route, I get this error.</p>
  
  <h2>Here is my controller :</h2>
  
  <pre><code>@CrossOrigin(
      origins = "*", 
      methods = {RequestMethod.POST, RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.DELETE}, 
      allowedHeaders = "*")
  @RestController
  @RequestMapping(path = "/user")
  public class UserController {
  
      @Autowired
      UserRepository userRepository;
  
      @Autowired
      ImageRepository imageRepsository;
  
  
  
      doesn't work--&gt;  @RequestMapping(value="/{userId}/getUserAndImage",method=RequestMethod.GET,produces = MediaType.IMAGE_JPEG_VALUE )
      public Optional&lt;User&gt; getUserAndImage(@PathVariable Long userId) {
  
      return userRepository.findById(userId);
  }
  
      works fine--&gt; @RequestMapping(value="/{userId}/image", method=RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
  public byte[] getUserImage(@PathVariable Long userId) {
      byte[] image = (imageRepsository.findImageWithUserId(userId)).getImage();
  
          return image;
  }
  </code></pre>
  
  <h2>Here are entities :</h2>
  
  <p>User entity : </p>
  
  <pre><code>@Entity
  @Table(name="users")
  public class User {
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  
  @NotNull
  @Size(max=100)
  @Column
  private String nom;
  
  @NotNull
  @Size(max=250)
  @Column
  private String prenom;
  
  
  @OneToOne(fetch=FetchType.EAGER,
            cascade = CascadeType.PERSIST)
  private Image image;
  
  //getters and setters
  }
  </code></pre>
  
  <p>Image entity :</p>
  
  <pre><code>@Entity
  @Table(name="images")
  public class Image {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  @Column(name="image")
  @Lob
  private byte[] image;
  
  @OneToOne(fetch = FetchType.EAGER)
  @JoinColumn(name="user_id")
  private User user;
  
  //getters and setters
  }
  </code></pre>