Spring - 调用RestService时的NoSuchMethodException

时间:2016-09-20 18:18:00

标签: spring

我有这个简单的映射,它应该返回一个List对象

    @RestController
@RequestMapping(value="/api")
public class ServerRESTController {

    @Autowired ServerService serverService;

    @RequestMapping(value="/server/{idServer}", method = RequestMethod.GET)
    public ResponseEntity<Server> getFloorLatUpdate(@PathVariable int idServer){

        Server server = serverService.findById(idServer);

        return new ResponseEntity<Server>(server, HttpStatus.OK);

    }

    @RequestMapping(value="/server/list", method = RequestMethod.GET)
    public ResponseEntity<List<Server>> listAllServers(){

        List<Server> servers = serverService.findAllServers(-1);

        return new ResponseEntity<List<Server>>(servers, HttpStatus.OK);

    }

}

Server.class是一个模型

@Entity
@Table(name = "server")
public class Server implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int serverId;
    private Piano piano;
    private String nomeServer;
    private String serverIp;
    private String descrizione;
    private boolean online;
    private Set<Interruttore> interruttori;
    private String firmwareVersion;

    public Server(){

    }

    public Server(int serverId, Piano piano, String nomeServer, String serverIp, String descrizione, boolean online,
            Set<Interruttore> interruttori, String firmwareVersion){
        this.serverId = serverId;
        this.piano = piano;
        this.nomeServer = nomeServer;
        this.descrizione = descrizione;
        this.serverIp = serverIp;
        this.online = online;
        this.interruttori = interruttori;
        this.setFirmwareVersion(firmwareVersion);
    }


    @Id
    @Column(name = "id_server", unique = true, nullable = false)
    @GeneratedValue(strategy = IDENTITY)
    public int getServerId() {
        return serverId;
    }
    public void setServerId(int serverId) {
        this.serverId = serverId;
    }

    @ManyToOne
    @JoinColumn(name="id_piano")
    public Piano getPiano() {
        return piano;
    }
    public void setPiano(Piano piano) {
        this.piano = piano;
    }

    @Column(name="nome_server")
    public String getNomeServer() {
        return nomeServer;
    }
    public void setNomeServer(String nomeServer) {
        this.nomeServer = nomeServer;
    }

    @Column(name="server_ip")
    public String getServerIp() {
        return serverIp;
    }
    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    @Column(name="descrizione")
    public String getDescrizione() {
        return descrizione;
    }
    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    @Column(name="online")
    public boolean isOnline() {
        return online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "server")
    public Set<Interruttore> getInterruttori() {
        return interruttori;
    }

    public void setInterruttori(Set<Interruttore> interruttori) {
        this.interruttori = interruttori;
    }

    @Column(name = "firmware_version")
    public String getFirmwareVersion() {
        return firmwareVersion;
    }

    public void setFirmwareVersion(String firmwareVersion) {
        this.firmwareVersion = firmwareVersion;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((descrizione == null) ? 0 : descrizione.hashCode());
        result = prime * result + ((nomeServer == null) ? 0 : nomeServer.hashCode());
        result = prime * result + (online ? 1231 : 1237);
        result = prime * result + ((piano == null) ? 0 : piano.hashCode());
        result = prime * result + serverId;
        result = prime * result + ((serverIp == null) ? 0 : serverIp.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Server other = (Server) obj;
        if (descrizione == null) {
            if (other.descrizione != null)
                return false;
        } else if (!descrizione.equals(other.descrizione))
            return false;
        if (nomeServer == null) {
            if (other.nomeServer != null)
                return false;
        } else if (!nomeServer.equals(other.nomeServer))
            return false;
        if (online != other.online)
            return false;
        if (piano == null) {
            if (other.piano != null)
                return false;
        } else if (!piano.equals(other.piano))
            return false;
        if (serverId != other.serverId)
            return false;
        if (serverIp == null) {
            if (other.serverIp != null)
                return false;
        } else if (!serverIp.equals(other.serverIp))
            return false;
        return true;
    }

}

尝试拨打服务时,我会:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Method [listAllServers] was discovered in the .class file but cannot be resolved in the class object

原因

java.lang.NoSuchMethodException: it.besmart.restcontroller.ServerRESTController.listAllServers()

我无法理解为什么会发生这种情况,我总是以这种方式使用ResponseEntity ......也许是列表中的?

1 个答案:

答案 0 :(得分:0)

请发布整个代码,以便我们了解。

这通常是在错误的位置调用方法时,或者在构建和运行时环境中存在不匹配时,或者在构造函数中错过参数等时。

此外,您可能需要检查类路径中的文件。由于某种原因,编译时间和实际运行时环境之间可能存在不匹配。例如,您说您使用命令行构建它。因此,可能存在一些差异,检查无害。

最后,你可以检查拼写错误 - 我知道这听起来很奇怪,但区分大小写非常重要。