L'applicazione è stata creata utilizzando SpringInizialitzr e come IDE uso Netbeans.
I moduli inclusi sono:
Thymeleaf
Spring Web
Spring Data JPA
Spring Web
Ho configurato lil file application.properties nel modo seguente:
spring.application.name=demoWorld
spring.datasource.url=jdbc:mysql://localhost:3306/world
spring.datasource.username=root
spring.datasource.password=cristiano
Il file persitence è invece configurato così:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="com.example_demoWold_war_0.0.1-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.example.demoWorld.Login</class>
<class>com.example.demoWorld.Country</class>
<class>com.example.demoWorld.Countrylanguage</class>
<class>com.example.demoWorld.City</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/world?zeroDateTimeBehavior=CONVERT_TO_NULL"/>
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="jakarta.persistence.jdbc.user" value="root"/>
<property name="jakarta.persistence.jdbc.password" value="cristiano"/>
</properties>
</persistence-unit>
</persistence>
Dopo ho creato l'interfaccia RepoCountry
package com.example.demoWorld;
import org.springframework.data.repository.CrudRepository;
/**
*
* @author paolo
*/
public interface RepoCountry extends CrudRepository<Country, Long> {
}
Dopo ancora ho creato la classe CountryController
package com.example.demoWorld;
import jakarta.persistence.Id;
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author paolo
*/
@RestController
public class CountryController {
private final RepoCountry repocountry;
public CountryController(RepoCountry _repocountry){
repocountry= _repocountry;
}
@GetMapping("/country")
public Iterable<Country> getCountry(){
return repocountry.findAll();
}
@PostMapping("/country/{code}")
public Optional<Country> getId(@PathVariable Long code){
return repocountry.findById(code);
}
}
Dopo con postman provo a fare la chiamata GET su questi tre indirizzi
http://127.0.0.1:8080/demoWorld
http://127.0.0.1:8080/demoWorld/country
http://127.0.0.1:8080/country
Ricevo sempre come risposta 404 page not found
Mille grazie per il tuo aiuto.