繁体   English   中英

如何解决 java.math.BigInteger 无法转换为 java.lang.Integer

[英]How to work around java.math.BigInteger cannot be cast to java.lang.Integer

我正在将一个小型应用程序从 TomCat 迁移到 WebLogic。 尝试部署到 weblogic 时,出现标题中显示的错误。 我查看了关于 SO 的类似问题,并基于它们更新了 mysql-connector 和 springboot 版本。 作为参考,这是我使用的。 Return type of JPA Repository 'getOne(id)' Method java.math.BigInteger cannot be cast to java.lang.Integer

我将附上一些代码来帮助查明问题。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>


    <artifactId>timeoff</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>timeoff</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.1.RELEASE</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>tomcat-embed-el</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>tomcat-embed-core</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>tomcat-embed-websocket</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.1.RELEASE</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

道.java

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Controller;

@Controller
public interface EmployeeRequestDao extends JpaRepository <EmployeeRequest, Long> {

}

应用.java

@ComponentScan 
@SpringBootApplication
public class TimeoffApplication extends SpringBootServletInitializer implements WebApplicationInitializer{

    @Autowired
    EmployeeRequestDao employeeRequestDao;
    public static void main(String[] args) {
        SpringApplication.run(TimeoffApplication.class, args);
    }

    @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
          return builder.sources(TimeoffApplication.class);




}

}

EmployeeRequest.java


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "requestdetails") 
public class EmployeeRequest {

    @Id
    @Column(name ="id")
    @GeneratedValue(strategy =GenerationType.AUTO)
    private Long id ;

    public EmployeeRequest(String selectedSupervisor2, String selectedLeave2, String name, String fromDate, String fromTime, String toDate, String toTime) {

        this.selectedSupervisor2 = selectedSupervisor2;
        this.selectedLeave2 = selectedLeave2;
        this.name = name;
        this.fromDate = fromDate;
        this.fromTime = fromTime;
        this.toDate = toDate;
        this.toTime = toTime;

    }



    public EmployeeRequest() {

    }


    @Column(name ="selectedSupervisor2")
    private String selectedSupervisor2;

    @Column(name ="selectedLeave2")
    private String selectedLeave2;

    @Column(name ="name")
    private String name;

    @Column(name ="fromDate")
    private String fromDate;

    @Column(name ="fromTime")
    private String fromTime;

    @Column(name ="toDate")
    private String toDate;


    @Column(name ="toTime")
    private String toTime;


    public String getSelectedSupervisor2() {
        return selectedSupervisor2;
    }

    public void setSelectedSupervisor2(String selectedSupervisor2) {
        this.selectedSupervisor2 = selectedSupervisor2;
    }

    public String getselectedLeave2() {
        return selectedLeave2;
    }

    public void setselectedLeave2(String selectedLeave2) {
        this.selectedLeave2 = selectedLeave2;
    }

    public Long getId() {
        return id;
    }


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


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getFromDate() {
        return fromDate;
    }


    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }


    public String getFromTime() {
        return fromTime;
    }


    public void setFromTime(String fromTime) {
        this.fromTime = fromTime;
    }


    public String getToDate() {
        return toDate;
    }


    public void setToDate(String toDate) {
        this.toDate = toDate;
    }


    public String getToTime() {
        return toTime;
    }


    public void setToTime(String toTime) {
        this.toTime = toTime;
    }

}

Controller.java



import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import EmployeeRequest;
import EmployeeRequestDao;


@RestController
@RequestMapping("api")
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
public class EmployeeController {

    @Autowired
    EmployeeRequestDao employeeRequestDao;

    /**
     * This is used to get all requests in Employee leave
     * 
     * @return
     */
    @GetMapping("items")
    public List<EmployeeRequest> getItems() {
        return employeeRequestDao.findAll();
    }

    /**
     * This method returns the requests by ID
     * 
     * @param id
     * @return
     */

    @GetMapping("item/{id}")
    public EmployeeRequest getItem(@PathVariable("id") Long id) {
        return employeeRequestDao.getOne(id);
    }

    /**
     * This method is used to add requests in the database
     * 
     * @param employeeRequest
     * @return
     */

    @PostMapping("items")
    public EmployeeRequest addItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This method is used to update requests.
     * 
     * ??? Can potentially implement this further for supervisor access to approve
     * or deny requests ???
     * 
     * @param employeeRequest
     * @return
     */

    @PutMapping("item")
    public EmployeeRequest saveOrUpdateItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This Method updates the requests by ID
     * 
     * @param id
     * @param employeeRequestDetails
     * @return
     */

    @PutMapping("item/{id}")
    public EmployeeRequest updateItemById(@PathVariable Long id,
            @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);

        employeeRequest.setName(employeeRequest.getName());
        employeeRequest.setFromDate(employeeRequest.getFromDate());
        employeeRequest.setFromTime(employeeRequest.getFromTime());
        employeeRequest.setToDate(employeeRequest.getToDate());
        employeeRequest.setToTime(employeeRequest.getToTime());

        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }

    /**
     * This method is used to delete all requests from db.
     * 
     * @param employeeRequest
     */

    @DeleteMapping("items")
    public void deleteAllItems(EmployeeRequest employeeRequest) {
        employeeRequestDao.deleteAll();
    }

    /**
     * This method deletes a request from db.
     * 
     * @param id
     * @return
     */
    @DeleteMapping("items/{id}")
    public String deleteItem(@PathVariable Long id) {
        employeeRequestDao.delete(id);
        return "Deleted";
    }

    /**
     * This method patch updates the Date and Time of requests.
     * 
     * @param partialUpdate
     * @param id
     * @return
     */
    @PatchMapping("item/{id}")
    public EmployeeRequest patchUpdateItemById(@PathVariable Long id, @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);
        employeeRequest.setFromDate(employeeRequestDetails.getFromDate());
        employeeRequest.setFromTime(employeeRequestDetails.getFromDate());
        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }




}


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import EmployeeRequest;
import EmployeeRequestDao;


@RestController
@RequestMapping("api")
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
public class EmployeeController {

    @Autowired
    EmployeeRequestDao employeeRequestDao;

    /**
     * This is used to get all requests in Employee leave
     * 
     * @return
     */
    @GetMapping("items")
    public List<EmployeeRequest> getItems() {
        return employeeRequestDao.findAll();
    }

    /**
     * This method returns the requests by ID
     * 
     * @param id
     * @return
     */

    @GetMapping("item/{id}")
    public EmployeeRequest getItem(@PathVariable("id") Long id) {
        return employeeRequestDao.getOne(id);
    }

    /**
     * This method is used to add requests in the database
     * 
     * @param employeeRequest
     * @return
     */

    @PostMapping("items")
    public EmployeeRequest addItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This method is used to update requests.
     * 
     * ??? Can potentially implement this further for supervisor access to approve
     * or deny requests ???
     * 
     * @param employeeRequest
     * @return
     */

    @PutMapping("item")
    public EmployeeRequest saveOrUpdateItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This Method updates the requests by ID
     * 
     * @param id
     * @param employeeRequestDetails
     * @return
     */

    @PutMapping("item/{id}")
    public EmployeeRequest updateItemById(@PathVariable Long id,
            @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);

        employeeRequest.setName(employeeRequest.getName());
        employeeRequest.setFromDate(employeeRequest.getFromDate());
        employeeRequest.setFromTime(employeeRequest.getFromTime());
        employeeRequest.setToDate(employeeRequest.getToDate());
        employeeRequest.setToTime(employeeRequest.getToTime());

        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }

    /**
     * This method is used to delete all requests from db.
     * 
     * @param employeeRequest
     */

    @DeleteMapping("items")
    public void deleteAllItems(EmployeeRequest employeeRequest) {
        employeeRequestDao.deleteAll();
    }

    /**
     * This method deletes a request from db.
     * 
     * @param id
     * @return
     */
    @DeleteMapping("items/{id}")
    public String deleteItem(@PathVariable Long id) {
        employeeRequestDao.delete(id);
        return "Deleted";
    }

    /**
     * This method patch updates the Date and Time of requests.
     * 
     * @param partialUpdate
     * @param id
     * @return
     */
    @PatchMapping("item/{id}")
    public EmployeeRequest patchUpdateItemById(@PathVariable Long id, @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);
        employeeRequest.setFromDate(employeeRequestDetails.getFromDate());
        employeeRequest.setFromTime(employeeRequestDetails.getFromDate());
        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }




}

堆栈跟踪

```Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:336) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 104 common frames omitted
Caused by: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1058) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:972) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:958) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:903) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1025) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3480) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2444) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:31) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at sun.reflect.GeneratedConstructorAccessor417.newInstance(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_221]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_221]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:395) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:383) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 105 common frames omitted
Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:976) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    ... 128 common frames omitted

看起来问题正在发生在铸造上。

因此,您拥有的 mysql 版本与您的 mysql-connector.jar 版本之间可能不匹配。 您可以尝试使用更新版本的 MySQL 连接器吗?

我还看到您正在使用 mysql-connector-jar:8.0.18。 但是,如果您看到堆栈跟踪,它会显示 mysql-connector-java-commercial-5.1.22-bin.jar。 所以冲突的 jar 也可能是一个问题。

您可以使用 BigInteger.intValue。 但请注意,如果您的 BigInteger 超出 int 的范围,您可能会丢失信息

在您的 EmployeeRequest class 尝试将字段 id 的类型更改为 BigInteger:

@Entity
@Table(name = "requestdetails") 
public class EmployeeRequest {

@Id
@Column(name ="id")
@GeneratedValue(strategy =GenerationType.AUTO)
private BigInteger id ;

public EmployeeRequest(String selectedSupervisor2, String selectedLeave2, String name, String fromDate, String fromTime, String toDate, String toTime) {

    this.selectedSupervisor2 = selectedSupervisor2;
    this.selectedLeave2 = selectedLeave2;
    this.name = name;
    this.fromDate = fromDate;
    this.fromTime = fromTime;
    this.toDate = toDate;
    this.toTime = toTime;

}



public EmployeeRequest() {

}


@Column(name ="selectedSupervisor2")
private String selectedSupervisor2;

@Column(name ="selectedLeave2")
private String selectedLeave2;

@Column(name ="name")
private String name;

@Column(name ="fromDate")
private String fromDate;

@Column(name ="fromTime")
private String fromTime;

@Column(name ="toDate")
private String toDate;


@Column(name ="toTime")
private String toTime;


public String getSelectedSupervisor2() {
    return selectedSupervisor2;
}

public void setSelectedSupervisor2(String selectedSupervisor2) {
    this.selectedSupervisor2 = selectedSupervisor2;
}

public String getselectedLeave2() {
    return selectedLeave2;
}

public void setselectedLeave2(String selectedLeave2) {
    this.selectedLeave2 = selectedLeave2;
}

public BigInteger getId() {
    return id;
}


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


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public String getFromDate() {
    return fromDate;
}


public void setFromDate(String fromDate) {
    this.fromDate = fromDate;
}


public String getFromTime() {
    return fromTime;
}


public void setFromTime(String fromTime) {
    this.fromTime = fromTime;
}


public String getToDate() {
    return toDate;
}


public void setToDate(String toDate) {
    this.toDate = toDate;
}


public String getToTime() {
    return toTime;
}


public void setToTime(String toTime) {
    this.toTime = toTime;
}

}

同时更新 JpaRepository 如下:

public interface EmployeeRequestDao extends JpaRepository <EmployeeRequest, BigInteger> {

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM