I have a method which inserts an object. But this object mapped with another object as one to many.
@Entity
public class FutureMessage {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String message;
@ManyToOne
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
This FutureMessage object has mapped to User object but there is a problem with post request.
{
"message":"qwerasdf",
"user":{"id":15,"username":"XXX"}
}
This request Returns 200 but in database XXX is not the right user name for id 15.
At the same time if user not exists in this request, it returns 200(OK) too.
My problem is this, I do not want to send user id. I just want to send message and username.(There are more than 2 attributes in FutureMessage objects)
@RequestMapping(path="message/new",method=RequestMethod.POST)
public FutureMessage newMessage(@RequestBody FutureMessage newMsg){// how to add user name here ?
FutureMessage msg = null;
if(newMsg==null)
return null;
if(newMsg.getUser()==null){
return null;
}
try{
msg = messageRepository.save(newMsg);
}catch(Exception ex){
ex.printStackTrace();
}
return msg;
}
EDIT : I only want to send FutureMessage object and username as ResponseBody. This is my question not returning 404 or anything else.