I can't understand how to resolve the error. It seems that all the parameters, functions and in general the path of my application with Angular Springboot (use Postgresql as the database) are correct, the database is connected correctly , on the Springboot side there is also the cors configured for be able to share resources.
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);
}
}
I have created a controller with two methods to create and get the "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);
}
}
*/
}
And as a model utente this
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);
}
The configuration on pom.xml seems correct
<?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>
And on the Angular side i have done apiutenti.service.ts and then the model and as last thing the appcomponent.ts
i tried to change the path of the angular and the springboot but nothing changed
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
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false,updatable = false)
it was easy to solve this problem in this case i have tried to increment a string obviously it causes errors, i have tried to eliminate the whole part of GeneratedValue and its strategy ,at the end works , thanks to myself