The request sent by the client was syntactically incorrect when running the SpringMVC project. Ideas for solving the problem?

Generally, The request sent by the client was syntactically incorrect. What are the problems that may occur?

In SpringMVC, when you send an HTTP request to the server, if the requested data cannot be parsed correctly on the server side, it may cause the error "Error 400: The request sent by the client was syntactically incorrect."

This error usually occurs in the following situations:

  1. Request parameters do not match the expected parameters on the server side: If the parameters in the request do not match the expected parameters on the server side, such as incorrect parameter types or missing parameters, etc., the server will not be able to correctly parse the request and trigger an error.
  2. Incorrect request body format: If the format of the request body is inconsistent with the expected format of the server, for example, the data sent does not meet the JSON, XML or form format requirements defined by the server, the server will not be able to correctly parse the request.
  3. Illegal characters used: If the request contains unsupported special characters or illegal characters, the server cannot process the request correctly.
  4. Server configuration problems: Sometimes, there may be problems with the server configuration, such as request size limits, etc., which may also cause this error.

In order to solve this problem, you can follow the following steps to troubleshoot:

  1. Check whether the request parameters are consistent with the expected parameters on the server side, including the name, type and number of parameters, etc.
  2. Check whether the format of the request body is correct, such as meeting the requirements of JSON, XML or form format.
  3. Check whether the request contains legal characters and avoid using unsupported special characters or illegal characters.
  4. Check whether there are any problems with the server configuration, such as whether the request size limit is set appropriately.

If the above troubleshooting methods cannot solve the problem, you can check the specific error log on the server side to obtain more detailed error information to help you quickly locate and solve the problem.

You can take a look at my question

When using SpringMVC to run your own project, I reported it

HTTP Status 400 -

The request sent by the client was syntactically incorrect.

mistake

At this time, many people will go back to find their own error reports and search for a lot of information. Often, they still cannot solve their problems after searching for a lot of information;

I also looked for a lot of questions and a lot of methods, so I ignored a very simple question. I will share it here! ! !

backend methods

I am taking a file upload as an example. Of course, the method is different but the principle is still the same.

@RequestMapping(value = "/upload")
    public String upload(StrutsClas strutsClas, MultipartFile pic) {
        try {

//            本地保存地址
            String dir = PropertiesUtil.getValue("dir");
//            网络保存地址
            String server = PropertiesUtil.getValue("server");

//            文件名
            String filename = pic.getOriginalFilename();
            FileUtils.copyInputStreamToFile(pic.getInputStream(), new File(dir + filename));
            //2) 更新数据库表文件记录
            strutsClas.setPic(server + filename);
            strutsClasBiz.updateByPrimaryKeySelective(strutsClas);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:list";
    }

front-end code

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图片上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/sc/upload" method="post" enctype="multipart/form-data">
    <label>班级编号:</label><input type="text" name="cid" readonly="readonly" value="${param.cid}"/><br/>
    <label>班级图片:</label><input type="file" name="pic"/><br/>
    <input type="submit" value="上传图片"/>
</form>
</body>
</html>

illustrate

You can see that the name of MultipartFile         in my method is pic , and the attribute name in the <input> tag in my front-end code form is also pic ; of course there is definitely no problem with this, but the problem lies here, everyone is sure You might think that this shouldn’t have any impact, but actually it doesn’t have any impact on the earth either .

Description of the problem

You can see that an entity class         is passed in my method . In fact, the attributes of this entity class correspond to the column names of my database table one-to-one .

Entity class

package com.tgq.model;

import lombok.ToString;

@ToString
public class StrutsClas {
    private Integer cid;

    private String cname;

    private String cteacher;

    private String pic;

    public StrutsClas(Integer cid, String cname, String cteacher, String pic) {
        this.cid = cid;
        this.cname = cname;
        this.cteacher = cteacher;
        this.pic = pic;
    }

    public StrutsClas() {
        super();
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCteacher() {
        return cteacher;
    }

    public void setCteacher(String cteacher) {
        this.cteacher = cteacher;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
}

Some people may think that my method of adding a path should be fine, but the problem lies in this obvious place.

problem solved

Because the name of MultipartFile in the method is pic , and the attribute name in the <input> tag in my front-end code form is also pic , we only need to change this pic.

    Because the name of MultipartFile     here must be the same as the name written in the attribute name in the <input> tag in the form form of the front-end code. Because we passed the entity class, our name cannot be the same as the attribute name of the entity class or our The column names of the database must be consistent, otherwise such problems will occur.

Changed code

method

@RequestMapping(value = "/upload")
    public String upload(StrutsClas strutsClas, MultipartFile zx) {
        try {
//            本地保存地址
            String dir = PropertiesUtil.getValue("dir");
//            网络保存地址/upload/
            String server = PropertiesUtil.getValue("server");
//            文件名
            String filename = zx.getOriginalFilename();
            FileUtils.copyInputStreamToFile(zx.getInputStream(), new File(dir + filename));
            //2) 更新数据库表文件记录
            strutsClas.setPic(server + filename);
            strutsClasBiz.updateByPrimaryKeySelective(strutsClas);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:list";
    }

front-end code

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图片上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/sc/upload" method="post" enctype="multipart/form-data">
    <label>班级编号:</label><input type="text" name="cid" readonly="readonly" value="${param.cid}"/><br/>
    <label>班级图片:</label><input type="file" name="zx"/><br/>
    <input type="submit" value="上传图片"/>
</form>
</body>
</html>

Test Results

Guess you like

Origin blog.csdn.net/weixin_74383330/article/details/132865533