【问题标题】:SPRING framework: The request sent by the client was syntactically incorrectSPRING 框架:客户端发送的请求语法错误
【发布时间】:2015-05-06 13:30:42
【问题描述】:
@RequestMapping(value = "/invoice", method = RequestMethod.POST)
    public String submitInvoice(HttpServletRequest request, 
            @RequestParam("clients") int clientId,
            @RequestParam("invoice_date") String invoice_date, 
            @RequestParam("invoice_due_date") String invoice_due_date, 
            @RequestParam("status") String status, 
            @RequestParam("payment_method") String payment_method, 
            @RequestParam("currency") String currency, 
            @RequestParam("description") String description, 
            @RequestParam("quantity") String quantity, 
            @RequestParam("price") String price, 
            @RequestParam("total") String lineTotal) {

        if(!hasRole(request, "ROLE_USER")){
            return "403";
        }

        long invoiceId = 0;

        DBManager.createInvoice(clientId, invoice_date, invoice_due_date, status, payment_method, currency);
        DBManager.invoiceDescription(invoiceId, description, quantity, price, lineTotal);



        return "redirect:/invoices/page/1";
    }

下面的数据库管理器

public static long createInvoice(
        int clientId, String invoice_date, String invoice_due_date, 
        String status, String payment_method, String currency){

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;

        long invoiceId = 0;

        //String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());


        String sql = "INSERT INTO invoice"
                + "(invoiceId, clientId, invoice_date, invoice_due_date, status, payment_method, currency) VALUES"
                + "(NULL,?,?,?,?,?,?)";

        try {

            dbConnection = getDBConnection();

            preparedStatement = dbConnection.prepareStatement(sql);

            preparedStatement.setInt(1, clientId);
            preparedStatement.setString(2, invoice_date);
            preparedStatement.setString(3, invoice_due_date);
            preparedStatement.setString(4, status);
            preparedStatement.setString(5, payment_method);
            preparedStatement.setString(6, currency);

            preparedStatement.executeUpdate();

            System.out.println("You have successfully created an invoice record");

            PreparedStatement getLastInsertId = dbConnection.prepareStatement("SELECT LAST_INSERT_ID()");
            ResultSet rs = getLastInsertId.executeQuery();
            if(rs.next())
            {
                invoiceId = rs.getLong("last_insert_id()");

                System.out.println("Last invoiceId inserted: " + invoiceId);

            }

        } catch (SQLException e){

            System.out.println(e.getMessage());

        }
        return invoiceId;
    }

public static void invoiceDescription(
        long invoiceId, String description, String quantity, 
        String price, String lineTotal){

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;


        //String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());


        String sql = "INSERT INTO invoice_description"
                + "(descId, invoiceId, description, quantity, price, total) VALUES"
                + "(NULL,?,?,?,?,?)";

        try {

            dbConnection = getDBConnection();

            preparedStatement = dbConnection.prepareStatement(sql);

            preparedStatement.setLong(1, invoiceId);
            preparedStatement.setString(2, description);
            preparedStatement.setString(3, quantity);
            preparedStatement.setString(4, price);
            preparedStatement.setString(5, lineTotal);

            preparedStatement.executeUpdate();

            System.out.println("You have successfully added descriptions to invoice");


        } catch (SQLException e){

            System.out.println(e.getMessage());

        }
    }

我收到上面代码的以下消息:

'The request sent by the client was syntactically incorrect.'

我正在尝试插入多个表。如果我删除DBManager.invoiceDescription,它将很好地将第一部分插入到发票表中而不会出现错误,但是当我添加invoiceDescription 部分时,它不会插入任何内容。请帮忙:)

【问题讨论】:

  • 请添加您遇到的错误的堆栈跟踪!!!

标签: java spring jsp spring-mvc


【解决方案1】:

错误"The request sent by the client was syntactically incorrect." 表示您的请求格式不正确。

我发现您的所有请求参数都是必需的,因此如果至少缺少其中一个参数,您将收到此错误。确保您发送所有请求参数,或者如果您有时想发送其中一些参数,您可以将它们设为非强制性:

@RequestParam(value = "clients", required = false)

在这种情况下,如果您将它们全部设为非强制性,您可以选择要发送的参数。

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2014-01-04
    • 1970-01-01
    相关资源
    最近更新 更多