HTTP Status 400 - The request sent by the client was syntactically incorrect. - within a Spring MVC web app using Hibernate

StackOverflow https://stackoverflow.com/questions/22130564

Question

I'm hoping somebody can help me this... I've been looking at this error for a while now.

I'm getting a "HTTP Status 400 - The request sent by the client was syntactically incorrect." error on the submit of a spring form.

The error is related to following code in my JSP spring form.

<form:input path="game_week_id"></form:input>

When I removed this line of code the error is not thrown (but of course I want to set the game_week_id from the form value entered). Just to note that game_week_id isn't actually an id (primary key) of the fixtures table - but just a name which I've given the column. The primary key for the table is actually an autonumber.

I want to insert a row into my fixtures table (based on the user form input) which is controlled by the Fixtures class.

I'm thinking I have something wrong in the binding of the game_week_id form field back to the object (or maybe the issue is the binding of game_week_id to the underlying database column).

Any direction or any help would be appreciated (go easy on me! - I'm aware this is probably something stupid I'm doing)

Code from my controller, Fixture object and JSP form is displayed below:

//CONTROLLER
@RequestMapping(value="/addFixtures", method=RequestMethod.POST)
public ModelAndView registerFixture(@ModelAttribute Fixture fixture, Map<String, Object>        map1, HttpServletRequest request1)
{
    String message;
    System.out.println("Adding fixture..");

    ModelAndView modelAndView = new ModelAndView();
    System.out.println("Fixture: Game Week ID " + fixture.getGame_week_id());
    System.out.println("Fixture: Game ID " + fixture.getGame_id());

    fixtureService.addFixture(fixture);
    modelAndView.setViewName("addFixtures");
    message = "Fixture successfully added";
    modelAndView.addObject("message", message);
    return modelAndView;
}

Fixture Class

    @Entity
    @Table(name = "fixtures")
    public class Fixture implements java.io.Serializable {

    private Integer id;
    public Gameweek game_week_id;
    private Integer game_id;
    private PPTeam home_team;
    private PPTeam away_team;

    public Fixture(){

    }

    public Fixture(
            Gameweek game_week_id, 
            Integer game_id,
            PPTeam home_team, PPTeam away_team, PPTeam homeTeam) {

        this.game_week_id = game_week_id;
        this.game_id = game_id;
        this.home_team = home_team;
        this.away_team = away_team;
    }

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name ="game_week_id") 
    public Gameweek getGame_week_id() {
        return game_week_id;
    }

    public void setGame_week_id(Gameweek game_week_id) {
        this.game_week_id = game_week_id;
    }

    public void setGame_week_id_int(Integer game_week_id_int) {
        this.game_week_id = game_week_id;
    }

    public Integer getGame_id() {
        return game_id;
    }

    public void setGame_id(Integer game_id) {
        this.game_id = game_id;
    }

    @ManyToOne
    @JoinColumn(name = "home_team", referencedColumnName = "teamId")
    public PPTeam getHome_team() {
        return this.home_team;
    }

    public void setHome_team(PPTeam home_team) {
        this.home_team = home_team;
    }

    @ManyToOne
    @JoinColumn(name = "away_team", referencedColumnName = "teamId")
    public PPTeam getAway_team() {
        return this.away_team;
    }

    public void setAway_team(PPTeam away_team) {
        this.away_team = away_team;
    }

    @Override
    public String toString() {
        return "Fixture [id=" + id + ", game_week_id=" + game_week_id
                + ", game_id=" + game_id + ", home_team=" + home_team
                + ", away_team=" + away_team + "]"; 
    }   
}

And finally my JSP form

<form:form method="POST" commandName="fixture" action="${pageContext.request.contextPath}/team/addFixtures">
    <table> 
        <tr>
            <td>Gameweek Number:</td>
            <td>
            <form:input path="game_week_id"></form:input>
            </td>
        </tr>
        <tr>
            <td>Game No:</td>
            <td>
            <form:input path="game_id"></form:input>
            </td>
        </tr>
    </table>
    <br>
    <input type="submit" value ="Add Fixture">
</form:form>
Was it helpful?

Solution

What is your GameWeek class? I thing that problem is that you try to bind text-field to an object GameWeek. When you send request with game_week_id it is string in Spring model map, and Spring cant make an GameWeek object from string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top