0

Hello i'm getting a 400 bad request when trying to access an endpoint. Im using spring mvc and mapped my url to my page but failing to access the endpoint when using window.location.href. Please what did i do wrong

Below is my code:

$.ajax({
            type: 'post',
            url: 'guest/search',

            data: JSON.stringify(formData),
            contentType: 'application/json',
            success: function(dataRecieved){
                var dataRecieved= $.trim(dataRecieved);
                //console.log(dataRecieved);
                if(dataRecieved === 'true'){
                    $("#statusPlaceholder").html("Great rooms are available");
                    window.location.href="/guestReservation"; //fails to go to the endpoint
                }else{
                    $("#statusPlaceholder").html("Sorry. Rooms are not available. Please try again").show().fadeOut(3000).css("color","red");
                }
            }

        });

My mapped endPoint:

@RequestMapping(value = "/guestReservation", method = RequestMethod.GET)
    public
    @ResponseBody
    String createGuest(@RequestBody Occupancy occupancy){
        return "guestReservation";
    }

This is my Occupancy class:

@Entity
@Table(name="Occupancy")
public class Occupancy implements Serializable {
    private static final long serialVersionUID = 15L;

    public Occupancy() {

    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="OCCUPANCYID", nullable = false)
    private long occupancyID;


    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="itinaryID")
    private Itinary itinary;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "roomNumber", referencedColumnName = "roomID")
    private Room room;

    @Column(name="roomsWanted")
    private int roomsWanted;

    public int getRoomsWanted() {
        return roomsWanted;
    }

    public void setRoomsWanted(int roomsWanted) {
        this.roomsWanted = roomsWanted;
    }

    //    @Column(name="status", columnDefinition = "default 0")
    private int status; // 0:reserved 1:cheched-in 2:checked-out

    private Date checkInDate;
    private Date checkOutDate;


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public long getOccupancyID() {
        return occupancyID;
    }

    public Itinary getItinary() {
        return itinary;
    }

    public void setItinary(Itinary itinary) {
        this.itinary = itinary;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

//    public int getNunOfPerson() {
//        return NunOfPerson;
//    }
//
//    public void setNunOfPerson(int nunOfPerson) {
//        NunOfPerson = nunOfPerson;
//    }

    public Date getCheckInDate() {
        return checkInDate;
    }

    public void setCheckInDate(Date checkInDate) {
        this.checkInDate = checkInDate;
    }

    public Date getCheckOutDate() {
        return checkOutDate;
    }

    public void setCheckOutDate(Date checkOutDate) {
        this.checkOutDate = checkOutDate;
    }

}
KENDRA SMITH
  • 95
  • 1
  • 1
  • 10
  • Can you directly access the full url in the browser (eg:- `http://www.something.com/guestReservation`) – Abhi Dec 15 '15 at 05:07
  • @Abhi you are right was actually trying that before mentioned it. Actually i could not access it – KENDRA SMITH Dec 15 '15 at 05:09
  • These links may help [Link 1](http://stackoverflow.com/a/19671511/2968762) [Link 2](http://pcsupport.about.com/od/browsers/fl/http-400-bad-request.htm). Try checking logs for debugging. I guess you are missing some parameters. – Abhi Dec 15 '15 at 05:13
  • try this ${pageContext.request.contextPath}/guestReservation and please give me to response what should be output is occuer. – sanjay Dec 15 '15 at 05:34
  • @sanjay i tried what u said and now i get a 404 error – KENDRA SMITH Dec 15 '15 at 05:56
  • In your web.xml file how to give *.htm same like or different please give me your url-pattern – sanjay Dec 15 '15 at 06:51
  • @sanjay im doing it in spring boot. When i remove the request body it seems to be no issue but now when i have the request body in it having problems – KENDRA SMITH Dec 15 '15 at 10:30

1 Answers1

0

I believe the issue is because of parameters expected in the /guestReservation API call.

I see in the method definition, the parameter is of type Guest. You need to check what is that you are expecting in this class.

Adding the required parameters should fix your issue.

Hope it helps!

hkasera
  • 2,118
  • 3
  • 23
  • 32
  • how can i add the required parameter. You are right its causing an issue – KENDRA SMITH Dec 15 '15 at 10:14
  • Check the *Guest* Class. If possible paste the contents of this class in the question to help you further. – hkasera Dec 15 '15 at 10:23
  • actually its occupancy class. I am trying to reach an endpoint and post data from a from. The model is an occupancy class – KENDRA SMITH Dec 15 '15 at 10:27
  • so i am originally sending a json response from spring either a true or false. When true i want to redirect me to the url /guestReservation. Occupation is just receiving a post from the url /guestReservation – KENDRA SMITH Dec 15 '15 at 10:49
  • You are explaining about /guest/search. I am asking about /guestReservation. In your method definition, you wrote **createGuest(@RequestBody Occupancy occupancy)** which means the GET request should have the payload JSON as mentioned in Occupancy class. If not you will receive 400. – hkasera Dec 15 '15 at 10:51
  • i have added the occupancy class – KENDRA SMITH Dec 15 '15 at 10:58