user1955934
user1955934

Reputation: 3505

Spring Rest Data - Foreign Key object URL not converted in @RequestBody

I'm using Spring Rest Data and I have configured my application to use RepositoryRestMvcConfiguration. I have the following 2 entities:

Enterprise have many User relationship.

@Entity
@Table(name="enterprise")
public class Enterprise extends BaseEntity
{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="enterprise_id")
    private Long id;

    @Column(name="enterprise_name")
    private String name;
}

@Entity
@Table(name="user")
public class User extends BaseEntity
{
    @Id
    @Column(name="user_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "owner_enterprise_id", referencedColumnName = "enterprise_id")
    private Enterprise ownerEnterprise;

    @Column(name="username")
    private String userName;

    @Column(name="emailaddress")
    private String emailAddress;

    @Column(name="passwordhash")
    private String password;
}

I was able to send post request to create enterprise, and the next thing i want to do is create a User that has a FK to the enterprise.

Here is my UserController:

@RequestMapping(value="/createUser", method=RequestMethod.POST)
public ResponseEntity<User> register(@RequestBody User user) {

    User newUser = userService.register(user);
    return new ResponseEntity<User>(newUser, HttpStatus.OK);
}

When I try to send post to create new user:

curl -i -X POST \
-H "Content-Type: application/json" \
-d '{"userName":"johndoe", "emailAddress":"[email protected]", "password":"johnpass1", "ownerEnterprise":"http://localhost:8080/enterprises/1"
}' \
http://localhost:8080/createUser

I put breakpoints in my controller and saw that the User object's ownerEnterprise field was null (but other fields contain correct data). Could someone please help show me what I am missing? I have tried to search this for hours and no luck.

I've also verified that the url is correct:

curl http://localhost:8080/enterprises/1 

    {
      "name" : "MyEnterprise",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/enterprises/1"
        }
      }
    }

Thanks a bunch for your time!!

Upvotes: 4

Views: 3619

Answers (2)

revobd
revobd

Reputation: 11

I ran into the same problem. Adding a constructor in your enterprise class should solve it.

public Enterprise(Long id) {
    super();
    this.id = id;

}

Upvotes: 1

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

Your interface is not a Spring-Data-REST based one, it is a straight Spring MVC request, so the following kind of a POST should work for you:

{"userName":"johndoe", "emailAddress":"[email protected]", "password":"johnpass1", "ownerEnterprise":{"id":1, "name":"Test Enterprise"}}

Essentially this is a json representation of your User entity.

Had you exposed your User entity as a @RepositoryRestResource then your original request should work

Upvotes: 2

Related Questions