简体   繁体   中英

How can I pass a class object in postman?

I am new to spring and hibernate and I've created a spring mvc application. The problem I'm having is with my Reservation Entity. When I call the add function from ReservationController in postman I am given a 400-Bad Request, The server cannot or will not process the request due to something that is perceived to be a client error (eg, malformed request syntax, invalid request message framing, or deceptive request routing). I believe it comes from hostId and guestId being a User type because my usercontroller.java's add method works fine but, I am not 100% certain that is the issue. If anyone knows how to resolve this please help.

I tried passing the information through postman as is like:

 {
        "confirmation": 4488,
        "guestId": "userId": 44,
        "hostId": "userId": 45,
        "checkInDate": "8/27/2019",
        "checkOutDate": "8/31/2019",
        "city": "Las Vegas",
        "state": "NV"
    }

and also like:

 {
        "confirmation": 4488,
        "guestId": [
                    "userId": 44,
                    "username": "username",
                    "password": "password",
                    "firstName": "name",
                    "lastName": "lastname",
                    "ssid": "WiFi",
                    "ssidPassword": "password!"],
        "hostId": [
                    "userId": 45,
                    "username": "user2",
                    "password": "password",
                    "firstName": "name2",
                    "lastName": "lastname2",
                    "ssid": "Red",
                    "ssidPassword": "red$1"],
        "checkInDate": "8/27/2019",
        "checkOutDate": "8/31/2019",
        "city": "Las Vegas",
        "state": "NV"
    }

but neither of those worked.

User.java

@Entity
@Table(name="User", schema="DB", uniqueConstraints = {@UniqueConstraint(columnNames= "Username")})
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="User_Id")
    private Integer userId;

    @Column(name="Username", unique=true)
    private String username;

    @Column(name="Password")
    private String password;

    @Column(name="First_Name")
    private String firstName;

    @Column(name="Last_Name")
    private String lastName;

    @Column(name="SSID")
    private String ssid;

    @Column(name="SSID_Password")
    private String ssidPassword;

    @OneToMany(mappedBy = "guestId", fetch = FetchType.EAGER)
    private Set<Reservation> guestReservations = new HashSet<>();

    @OneToMany(mappedBy= "hostId", fetch = FetchType.EAGER)
    private Set<Reservation> hostReservations = new HashSet<>();

    ...

Reservation.java


@Entity
@Table(name="Reservations", schema="DB")
public class Reservation implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="Confirmation_Num")
    private Integer confirmation;

    @JoinColumn(name="Guest_Id")
    @ManyToOne
    private User guestId;

    @JoinColumn(name="Host_Id")
    @ManyToOne
    private User hostId;

    @Column(name="Check_In_Date", unique=true)
    private String checkInDate;

    @Column(name="Check_Out_Date", unique=true)
    private String checkOutDate;

    @Column(name="City")
    private String city;

    @Column(name="State")
    private String state;

...

ReservationRepository.java


@Transactional
@EnableTransactionManagement
@Repository
public class ReservationRepository {

    @Autowired
    SessionFactory sf;

    //Creates a new reservation
    public String add(Reservation res) {
        Session s = sf.getCurrentSession();
        s.persist(res);
        return "Reservation Requested";
    }

    public List<Reservation> selectAll(){
        List<Reservation> res = new ArrayList<Reservation>();
        Session s = sf.getCurrentSession();
        Criteria cr = s.createCriteria(Reservation.class);
        res = cr.list();
        return res;
    }

    public void update(Reservation change) {
        Session s = sf.getCurrentSession();
        s.update(change);
    }

UserRepository.java


@Transactional
@EnableTransactionManagement
@Repository
public class UserRepository {

    @Autowired
    SessionFactory sf;

    //Allows you to create a new user
    public String add(User user) {
        Session s= sf.getCurrentSession();
        s.persist(user);
        return "user created";
    }

    public List<User> selectAll(){
        List<User> users = new ArrayList<User>();
        Session s = sf.getCurrentSession();
        Criteria cr = s.createCriteria(User.class);
        users = cr.list();
        return users;
    }

    public void update(User change) {
        Session s = sf.getCurrentSession();
        s.update(change);
    }

    public void delete(User usr) {
        Session s = sf.getCurrentSession();
        s.delete(usr);
    }

    public List<User> select(String username){
        List<User> user = new ArrayList<User>();
        Session s = sf.getCurrentSession();
        Criteria cr = s.createCriteria(User.class);
        cr.add(Restrictions.eq("username", username));
        user = cr.list();
        return user;
    }


ReservationController.java


@Controller
@RequestMapping("/Reservation")
@CrossOrigin(origins="*", allowedHeaders="*", value="*")
public class ReservationController {

    @Autowired
    ReservationService resServ;

    @PostMapping(value="/add")
    @ResponseBody
    public void add(@RequestBody Reservation res) {
        resServ.add(res);
    }

    @GetMapping("/all")
    public ResponseEntity<List<Reservation>> selectAll(){
        return new ResponseEntity<>(resServ.selectAll(), HttpStatus.OK);
    }

    @PutMapping("/update")
    public void update(@RequestBody Reservation change) {
        resServ.update(change);
    }

    @DeleteMapping("/delete")
    public void delete(@RequestBody Reservation res) {
        resServ.delete(res);
    }

    @GetMapping(value="/selectByGuest/{guestId}")
    public ResponseEntity<List<Reservation>> selectAllForGuest(@PathVariable String guestId){
        return new ResponseEntity<>(resServ.selectAllForGuest(guestId), HttpStatus.OK);

    }

    @GetMapping(value="/selectByHost/{hostId}")
    public List<Reservation> selectAllForHost(@PathVariable String hostId){
        return resServ.selectAllForHost(hostId);
    }
}

UserController.java

@Controller
@RequestMapping("/User")
@CrossOrigin(origins="*", allowedHeaders="*", value="*")
public class UserController {

    @Autowired
    UserService usrServ;

    @PostMapping(value="/add")
    @ResponseBody
    public void add(@RequestBody User usr) {
        usrServ.add(usr);
    }

    @GetMapping(value="/all")
    public ResponseEntity<List<User>> selectAll(){
        return new ResponseEntity<>(usrServ.selectAll(), HttpStatus.OK);
    }


    @PutMapping(value="/update")
    public void update(@RequestBody User change) {
        usrServ.update(change);
    }


    @DeleteMapping(value="/delete")
    public void delete(@RequestBody User usr) {
        usrServ.delete(usr);
    }

    @GetMapping(value="/select/{username}")
    public ResponseEntity<List<User>> select(@PathVariable String username){
        return new ResponseEntity<>(usrServ.select(username), HttpStatus.OK);
    }

    @GetMapping(value="/selectSSID/{username}")
    public ResponseEntity<String> selectSSID (@PathVariable String username){
        return new ResponseEntity<>(usrServ.selectSSID(username), HttpStatus.OK); 
    }

    @GetMapping(value="/selectSSIDP/{username}")
    public ResponseEntity<String> selectSSIDPassword (@PathVariable String username){
        return new ResponseEntity<>(usrServ.selectSSIDPassword(username), HttpStatus.OK); 
    }
}

For your case controller expects valid JSON representation of User object. The one that you posted in question is not valid JSON string.

Json format docs

To get valid representation you could do following:

User user = new User();
user.setConfirmation(123);
ObjectMapper objectMapper = new ObjectMapper();
System.out.printLn(objectMapper.writeValueAsString(user));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM