问题 HttpErrorResponse 错误 500

问题描述 投票:0回答:1

我不明白如何解决该错误。看来我的 Angular Springboot 应用程序的所有参数、功能和路径(使用 Postgresql 作为数据库)都是正确的,数据库连接正确,在 Springboot 端还配置了能够共享的 cors资源。

package esercizio3.esercizio3.Configurations;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/utenti")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET")
                .allowedHeaders("*") // Allow all headers
                .allowCredentials(false);
    }
}

我创建了一个控制器,有两种方法来创建和获取“utente”。

package esercizio3.esercizio3.Controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import esercizio3.esercizio3.model.utente;
import esercizio3.esercizio3.utenteREPO.utenteREPO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;


@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class utentecontroller {

  @Autowired
  utenteREPO utenterep;


  @GetMapping("/utenti")
  public  ResponseEntity<List<utente>> getAllutenti(@RequestParam (required = false)String email) {
    try {
      List<utente>utenti=new ArrayList<utente>();

      if(email==null)
        utenterep.findAll().forEach(utenti::add);
        else{
          utenterep.findByEmail(email).forEach(utenti::add);
          if (utenti.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT );
          }
        }
         return new ResponseEntity<>(utenti, HttpStatus.OK);
    } catch (Exception e) {
      return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
    }

  }

  @PostMapping("/utenti")
  public ResponseEntity<utente> createutente(@RequestBody utente utente) {
      try {
          utente _utente = utenterep.save(new utente(
                  utente.getEmail(),
                  utente.getNome(),
                  utente.getcognome(),
                  utente.getCittadiresidenza(),
                  utente.getCodicefiscale(),
                  utente.getVia(),
                  utente.getProvinciadiresidenza()
          ));
          return new ResponseEntity<>(_utente, HttpStatus.CREATED);
      } catch (Exception e) {
        
          return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
      }
  }

  /*@PostMapping("/utenti")
public ResponseEntity<utente> createutente(@RequestBody utente utente) {
    try {
        utente _utente = utenterep.save(new utente(
                utente.getEmail(),
                utente.getNome(),
                utente.getCognome(),
                utente.getCittadiresidenza(),
                utente.getCodicefiscale(),
                utente.getVia(),
                utente.getProvinciadiresidenza()
        ));
        return new ResponseEntity<>(_utente, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
 */

}

作为一个模型utente这个

package esercizio3.esercizio3.model;

import java.io.Serializable;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class utente implements Serializable{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(nullable = false,updatable = false)

  private String email;

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

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

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

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

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

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

  public utente() {
  }

  public utente(String email, String nome, String cognome, String codicefiscale, String provinciadiresidenza, String cittadiresidenza, String via) {
    this.email = email;
    this.nome = nome;
    this.cognome = cognome;
    this.codicefiscale = codicefiscale;
    this.provinciadiresidenza = provinciadiresidenza;
    this.cittadiresidenza = cittadiresidenza;
    this.via = via;
  }

  public String getEmail() {
    return this.email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getNome() {
    return this.nome;
  }

  public void setNome(String nome) {
    this.nome = nome;
  }

  public String getcognome() {
    return this.cognome;
  }

  public void setcognome(String cognome) {
    this.cognome = cognome;
  }

  public String getCodicefiscale() {
    return this.codicefiscale;
  }

  public void setCodicefiscale(String codicefiscale) {
    this.codicefiscale = codicefiscale;
  }

  public String getProvinciadiresidenza() {
    return this.provinciadiresidenza;
  }

  public void setProvinciadiresidenza(String provinciadiresidenza) {
    this.provinciadiresidenza = provinciadiresidenza;
  }

  public String getCittadiresidenza() {
    return this.cittadiresidenza;
  }

  public void setCittadiresidenza(String cittadiresidenza) {
    this.cittadiresidenza = cittadiresidenza;
  }

  public String getVia() {
    return this.via;
  }

  public void setVia(String via) {
    this.via = via;
  }





  @Override
  public String toString() {
    return "{" +
      " email='" + getEmail() + "'" +
      ", Nome='" + getNome() + "'" +
      ", cognome='" + getcognome() + "'" +
      ", codicefiscale='" + getCodicefiscale() + "'" +
      ", provinciadiresidenza='" + getProvinciadiresidenza() + "'" +
      ", cittadiresidenza='" + getCittadiresidenza() + "'" +
      ", via='" + getVia() + "'" +
      "}";
  }
}

package esercizio3.esercizio3.utenteREPO;





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

import esercizio3.esercizio3.model.utente;
import java.util.List;


public interface utenteREPO extends JpaRepository<utente,String> {

  List<utente> findByEmail(String email);
  List<utente> findByNome(String nome);


}

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>esercizio3</groupId>
    <artifactId>esercizio3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>esercizio3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

  

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在 Angular 方面,我完成了 apiutenti.service.ts,然后是模型,最后是 appcomponent.ts

我试图改变角度和弹簧靴的路径,但没有任何改变

HttpErrorResponse
error
: 
null
headers
: 
_HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
: 
"Http failure response for http://localhost:8080/api/utenti: 500 OK"
name
: 
"HttpErrorResponse"
ok
: 
false
status
: 
500
statusText
: 
"OK"
url
: 
"http://localhost:8080/api/utenti"
[[Prototype]]
: 
HttpResponseBase
angular spring-boot rest registration http-status-code-500
1个回答
0
投票
@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(nullable = false,updatable = false)

在这种情况下解决这个问题很容易,我尝试增加一个字符串,显然它会导致错误,我尝试消除GenerateValue的整个部分及其策略,最后工作,感谢我自己

© www.soinside.com 2019 - 2024. All rights reserved.