Java 未正确将字符串转换为长对象

发布于 2024-07-09 13:14:41 字数 1696 浏览 11 评论 0 原文

我们在 AIX 的 Websphere 应用程序服务器上使用 Spring/Hibernate。 在我的 Windows 机器上,只有在 AIX 上运行时才不会出现此问题。 当用户使用帐号登录时,如果他们在登录 ID 前添加“0”前缀,应用程序将拒绝登录。 在 DB2 表中,列是数字类型,将 '090....' 转换为 '90...' 应该不会有问题

其他人也遇到过这样的问题吗? 两台机器都有 Java v1.5。

更具体的说,流程是FormView -> 登录验证器-> LoginController

在 LoginValidator 中,login 的值为 null,带有前缀 0。如果没有 0,该值就是它应该的值(但同样,这仅在 AIX 环境上 - 在 2 个 Windows 环境上没问题)。 下面是对象等于 null 的代码片段。

public class LoginValidator implements Validator  {

    public boolean supports(Class clazz) {
    return Login.class.equals(clazz);
    }

    @SuppressWarnings("all")
    public void validate(Object obj, Errors errors) {
        System.out.println("Inside LoginValidator");
        Login login = (Login) obj;
        //null value
        System.out.println("Before conversion in Validator, store id = " 
              + login.getStoreId()); 
    }
}

我还编写了这个简短的 Java 程序,用于从字符串构造 Long,并使用与 WebSphere

public class String2Long {
    public static void main(String[] args){
        String a = "09012179";
        String b = "9012179";

        Long _a = new Long(a);
        Long _b = new Long(b);

        System.out.println(a + " => " + _a); //09012179 => 9012179
        System.out.println(b + " => " + _b); //9012179 => 9012179
        System.out.println("_a.equals(_b) " + _a.equals(_b)); //_a.equals(_b) true
    }
}

解决方案

We're using Spring/Hibernate on a Websphere Application Server for AIX. On my Windows machine, the problem doesn't occur--only when running off AIX. When a user logs in with an account number, if they prefix the '0' to their login ID, the application rejects the login. In the DB2 table, the column is of numeric type, and there shouldn't be a problem converting '090....' to '90...'

Anyone else experience a problem like this? Both machines have Java v1.5.

To be more specific, the flow is FormView -> LoginValidator -> LoginController

In LoginValidator, the value of login is null with the prefixed 0. Without the 0, the value is what it should be (But again, this is only on the AIX environment--on 2 Windows environments it's fine). Here's the snippet of code where the object equals null..

public class LoginValidator implements Validator  {

    public boolean supports(Class clazz) {
    return Login.class.equals(clazz);
    }

    @SuppressWarnings("all")
    public void validate(Object obj, Errors errors) {
        System.out.println("Inside LoginValidator");
        Login login = (Login) obj;
        //null value
        System.out.println("Before conversion in Validator, store id = " 
              + login.getStoreId()); 
    }
}

I've also written this short Java program for constructing a Long from a String, and using the java binary that is packaged with WebSphere

public class String2Long {
    public static void main(String[] args){
        String a = "09012179";
        String b = "9012179";

        Long _a = new Long(a);
        Long _b = new Long(b);

        System.out.println(a + " => " + _a); //09012179 => 9012179
        System.out.println(b + " => " + _b); //9012179 => 9012179
        System.out.println("_a.equals(_b) " + _a.equals(_b)); //_a.equals(_b) true
    }
}

SOLUTION

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2024-07-16 13:14:41

嗯,那里发生了很多事情。 您确实需要尝试隔离问题 - 弄清楚发送到数据库的内容、Java 看到的内容等。

尝试将其固定在一个简短但完整的程序中,只是显示问题 -那么您将处于更有利的位置来提交错误或修复代码。

Well there's an awful lot of things going on there. You really need to try to isolate the problem - work out what's being sent to the database, what's being seen by Java etc.

Try to pin it down in a short but complete program which just shows the problem - then you'll be in a much stronger position to file a bug or fix your code.

鱼窥荷 2024-07-16 13:14:41

解决方案

一位同事对 Spring 更新进行了一些研究,显然这个错误在 v.2.5.3 中是正确的:

CustomNumberEditor 将带有前导零的数字视为十进制(删除了不需要的八进制支持,同时保留十六进制)

我们使用的是 Spring 2.0.5。 我们只是用 Spring 2.5.4 替换了 jars,它就按预期工作了!

感谢大家的帮助/协助。 我们将来会使用单元测试,但这只是 Spring 的一个 bug。

SOLUTION

A co-worker did some research on Spring updates, and apparently this error was correct in v. 2.5.3:

CustomNumberEditor treats number with leading zeros as decimal (removed unwanted octal support while preserving hex)

We were using Spring 2.0.5. We simply replaced the jars with Spring 2.5.4, and it worked as it should have!

Thanks to everyone for your help/assistance. We will make use of Unit tests in the future, but this just turned out to be a Spring bug.

九局 2024-07-16 13:14:41

沿着字符串的路径跟踪程序一直到数据库,并对该路径上的每个方法进行单元测试。 并且不要只在这里采取最短的路径,使用不同的输入和预期输出进行多个单元测试,以真正了解可能出了什么问题。 假设您没有发现任何错误,请在另一台计算机上运行相同的单元测试,您应该能够查明错误。 从我的头脑中,我认为这可能与区分大小写有关,但确实没有办法确定。

下次,使用 TDD。

Trace through the program following the path of the String all the way to database and make unit tests for every single method on that path. And don't just take the shortest possible route here, make multiple unit tests with different inputs and expected outputs to really see what went possibly wrong. Assuming you don't find any errors, run the same unit tests on the other computer and you should be able to pinpoint the bug. From the top of my head I'd assume it may have something to do with case sensitivity but there really is no way to be sure.

Next time, use TDD.

戏蝶舞 2024-07-16 13:14:41

我对Java了解不多,但是由于前导“0”,字符串可能会被解释为八进制字符串。

您可以使用 Long.parseLong(a, 10) 解决此问题。

I don't know much about Java, but this might happen the string is interpreted as octal string because of the leading "0".

You can probably work around this using Long.parseLong(a, 10).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文