1

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.

Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26

2 Answers2

0

Spring has parameterized object named ResponseEntity that you should return from your controller methods. It allows you to programmatically send different HTTP responses based on the request. Look at this post for an example of returning a 400 response when the request is bad, in your case when the User is null.

As for the mismatch between user XXX and id 15 I don't fully understand what your problem is. It sounds like you just want to send in the message and username? if that is the case your repository needs to be smart enough to check on username and the usernames better be unique or you'll get collisions.

0

Responding to your first problem. Even though you return null, it will be SUCCESS (Status code - 200) http request. If you specifically want to send the request as bad request when you find your logic returns null, then return like

return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);

How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?

Second, when you don't want to send the user id. The make sure you have configured the correct relationship between your models (message, user ) and you have placed the user Id's generated type to any of your Id generation strategy such as auto increment

Community
  • 1
  • 1
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28