在java中将字符串转换为长字符串

在java中将字符串转换为长字符串,java,spring-boot,Java,Spring Boot,我正在处理一个spring boot项目,我正在尝试在控制器中创建一个路由,该路由使用动态数量的参数。路线如下所示: @RequestMapping(value = "/addItem", method = RequestMethod.POST) public String addItem(ModelMap model, @RequestParam Map<String,String> allRequestParams) { String

我正在处理一个spring boot项目,我正在尝试在控制器中创建一个路由,该路由使用动态数量的参数。路线如下所示:

@RequestMapping(value = "/addItem", method = RequestMethod.POST)
    public String addItem(ModelMap model, @RequestParam Map<String,String> allRequestParams) {
        String stringId = allRequestParams.get("id");
        System.out.println(stringId);
        long id = Long.valueOf(stringId);
        System.out.println(id);
        ...
    }
<form method="POST" action="addItem">
            <div class="form-group">
                <table id="details" class="table table-responsive">
                    <tr id="type">
                        <th class="text-left select-title">Type* :</th>
                        <td>
                            <select name="id" id="input" class="form-control form-control-sm inv-select" required>
                                <option value="" disabled selected>Selected...</option>
                                <%@ include file = "common/dropdown.jsp" %>
                            </select>
                        </td>   
                    </tr>
                    <tr id="component1">
                        <th class="text-left select-title">Type of Component * :</th>
                        <td>
                            <select id="select" name="component1" class="form-control form-control-sm inv-select" required>
                                <option value="" disabled selected>Selected...</option>
                                <%@ include file = "common/dropdown.jsp" %>
                            </select>
                        </td>
                        <th class="text-left quant-title">Quantity * :</th>
                        <td class="quantity-field">
                            <input name="quantity1" type="number" step="any" placeholder="Quantity" class="quantity-input" required>
                        </td>
                    </tr>
                    <tr class="table-row">
                        <td>
                            <button onclick="addComponent()" id="add-component" class="btn btn-md btn-outline-success"> + Add Component</button><br>
                        </td>
                        <td>
                            <button onclick="removeComponent()" id="rem-component" class="btn btn-md btn-outline-danger"> - Remove Component</button><br>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <input type="submit" name="submit" id="submit">
                        </th>
                    </tr>
                  </table>
                </div>
</form>
我从这样的表格中调用路线:

@RequestMapping(value = "/addItem", method = RequestMethod.POST)
    public String addItem(ModelMap model, @RequestParam Map<String,String> allRequestParams) {
        String stringId = allRequestParams.get("id");
        System.out.println(stringId);
        long id = Long.valueOf(stringId);
        System.out.println(id);
        ...
    }
<form method="POST" action="addItem">
            <div class="form-group">
                <table id="details" class="table table-responsive">
                    <tr id="type">
                        <th class="text-left select-title">Type* :</th>
                        <td>
                            <select name="id" id="input" class="form-control form-control-sm inv-select" required>
                                <option value="" disabled selected>Selected...</option>
                                <%@ include file = "common/dropdown.jsp" %>
                            </select>
                        </td>   
                    </tr>
                    <tr id="component1">
                        <th class="text-left select-title">Type of Component * :</th>
                        <td>
                            <select id="select" name="component1" class="form-control form-control-sm inv-select" required>
                                <option value="" disabled selected>Selected...</option>
                                <%@ include file = "common/dropdown.jsp" %>
                            </select>
                        </td>
                        <th class="text-left quant-title">Quantity * :</th>
                        <td class="quantity-field">
                            <input name="quantity1" type="number" step="any" placeholder="Quantity" class="quantity-input" required>
                        </td>
                    </tr>
                    <tr class="table-row">
                        <td>
                            <button onclick="addComponent()" id="add-component" class="btn btn-md btn-outline-success"> + Add Component</button><br>
                        </td>
                        <td>
                            <button onclick="removeComponent()" id="rem-component" class="btn btn-md btn-outline-danger"> - Remove Component</button><br>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <input type="submit" name="submit" id="submit">
                        </th>
                    </tr>
                  </table>
                </div>
</form>

类型*:
挑选出来的。。。
组件类型*:
挑选出来的。。。
数量*:
+添加组件
-移除组件

控制器路由中的print语句用于调试,仅打印第一条print语句。如何正确地将字符串转换为长字符串?

您说过字符串是
“1\r\n”

但是,仅适用于仅由数字组成的字符串

要解决此问题,请使用:

Long.valueOf(stringId.trim());
我建议您使用instead of,因为它返回
long
而不是
long
。这样可以消除无用的对象创建

@RequestMapping(value = "/addItem", method = RequestMethod.POST)
public String addItem(ModelMap model, @RequestParam Map<String,String> allRequestParams) {
    String stringId = allRequestParams.get("id");
    System.out.println(stringId);
    long id = Long.parseLong(stringId.trim());
    System.out.println(id);
    ...
}
@RequestMapping(value=“/addItem”,method=RequestMethod.POST)
公共字符串附加项(ModelMap模型,@RequestParam映射allRequestParams){
String stringId=allRequestParams.get(“id”);
System.out.println(stringId);
long id=long.parseLong(stringId.trim());
系统输出打印项次(id);
...
}

看起来你的输入字符串实际上是
“\”1”
,而不是
“1”
。你能用调试器验证吗?用curl-X POST/addItem做一个测试吗?id=1你的调用看起来怎么样?@knittl我想你在调试时是对的,我发现
id
的值是
“1\r\n”
(id=718)@VictorPoloDeGyvesMontero使用curl调用进行测试,我认为问题在于发送到路由的形式