Hello I am struggling to figure out what is my issue. Teacher did this in eclipse. I am on Intelleji. I made a project with Java 17 and the SDK 17 too. Spring boot 3.0.0 m1
It is basic code but when I try to get it working i get error 404. Driving me nuts. If anyone sees something that I am not seeing i would be extremely grateful.
the link i try in postman is as follow: localhost:8081/restws/services/patientservice/patients
which is what the teacher did in eclipse or string tool suite.
I am in intelleji ultimate.
thank you for any help.
my structure goes as follow:
.
└── restws
├── PatientService.java
├── PatientServiceImpl.java
├── RestwsApplication.java
└── model
└── Patient.java
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.0.0-M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.slinky</groupId>
<artifactId>restws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restws</name>
<description>restws</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
PatientService.java
package com.slinky.restws;
import com.slinky.restws.model.Patient;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import java.util.List;
#Path("/patientservice")
public interface PatientService {
#Path("/patients")
#GET
List<Patient> getPatient();
}
Patient.java from com.slinky.restws.model
package com.slinky.restws.model;
import jakarta.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "Patient")
public class Patient {
private long id;
private String name;
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;
}
}
application.properties
cxf.jaxrs.classes-scan=true
cxf.jaxrs.classes-scan-packages=org.codehaus.jackson.jaxrs, com.slinky.restws
server.servlet.context-path=/restws
server.port=8081
PatientServiceImpl.java
package com.slinky.restws;
import com.slinky.restws.model.Patient;
import org.springframework.stereotype.Service;
import java.util.*;
#Service
public class PatientServiceImpl implements PatientService{
Map<Long, Patient> patients = new HashMap<>();
long currentId = 123;
public PatientServiceImpl() {
init();
}
public void init() {
Patient patient = new Patient();
patient.setId(currentId);
patient.setName("John");
patients.put(patient.getId(), patient);
}
#Override
public List<Patient> getPatient() {
Collection<Patient> results = patients.values();
List<Patient> response = new ArrayList<>(results);
return response;
}
}
main which was created automatically
package com.slinky.restws;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class RestwsApplication {
public static void main(String[] args) {
SpringApplication.run(RestwsApplication.class, args);
}
}
Related
After days of search over internet and many tries(out of which nothing worked out for me), i am writing here
I have Project- spring-cloud-config-server which has following files(full project can be accessed from https://github.com/AshishBharadwaj94/spring-cloud-config-server.git)
SpringCloudConfigServerApplication.java
application.properties (under folder- resources)
Link Source Folder- git-config-repo which has limits-service's properties files
pom.xml
and Client Config Project- limits-service which has following files(full project can be accessed from https://github.com/AshishBharadwaj94/limits-service.git)
LimitsServiceApplication.java
LimitConfiguration.java
Configuration.java
LimitsConfigurationController.java
bootstrap.properties (under folder- resources)
pom.xml
Also project- https://github.com/AshishBharadwaj94/git-config-repo.git
Project- spring-cloud-config-server is working fine and has the following output
// http://localhost:8888/limits-service/default
{
"name": "limits-service",
"profiles": [
"default"
],
"label": null,
"version": "8dd340030a5c82a11327bf0d2e55ae3b6434240b",
"state": null,
"propertySources": [
{
"name": "file:///D:/Learn-Java/springboot-workspace/git-config-repo//limits-service.properties",
"source": {
"limits-service.minimum": "11",
"limits-service.maximum": "99"
}
}
]
}
but when running Project- limits-service, i have the following output. It is unable to fetch data from spring-cloud-config-server
// http://localhost:8080/limits
{
"minimum": null,
"maximum": null
}
Below are the files Project wise
spring-cloud-config-server
SpringCloudConfigServerApplication.java
#EnableConfigServer
#SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
application.properties
spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///D:/Learn-Java/springboot-workspace/git-config-repo/
spring-cloud-config-server/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>2.4.0-M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.microservices</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
limits-service.properties
limits-service.minimum=11
limits-service.maximum=99
limits-service
LimitsServiceApplication.java
#SpringBootApplication
public class LimitsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(LimitsServiceApplication.class, args);
}
}
LimitConfiguration.java
#Component
public class LimitConfiguration {
private String minimun;
private String maximun;
public LimitConfiguration() {
super();
}
public LimitConfiguration(String minimun, String maximun) {
super();
this.minimun = minimun;
this.maximun = maximun;
}
public String getMinimun() {
return minimun;
}
public void setMinimun(String minimun) {
this.minimun = minimun;
}
public String getMaximun() {
return maximun;
}
public void setMaximun(String maximun) {
this.maximun = maximun;
}
#Override
public String toString() {
return "LimitConfiguration [minimun=" + minimun + ", maximun=" + maximun + "]";
}
}
Configuration.java
#Component
#ConfigurationProperties("limits-service")
public class Configuration {
private String minimum;
private String maximum;
public String getMinimum() {
return minimum;
}
public void setMinimum(String minimum) {
this.minimum = minimum;
}
public String getMaximum() {
return maximum;
}
public void setMaximum(String maximum) {
this.maximum = maximum;
}
}
LimitsConfigurationController.java
#RestController
public class LimitsConfigurationController {
#Autowired
private Configuration configuration;
#GetMapping(path="/limits")
public LimitConfiguration retrieveLimitsFromConfiguration() {
return new LimitConfiguration(configuration.getMinimum(), configuration.getMaximum());
}
}
bootstrap.properties
spring.application.name=limits-service
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled=true
limits-service/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>2.4.0-M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.microservices</groupId>
<artifactId>limits-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>limits-service</name>
<description>Build microservices with spring cloud</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
I can upload one file to a spring boot controller like this:
#RequestMapping(value = "/projects",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
public ResponseEntity<Void> upload(#RequestParam(value="file", required=false) MultipartFile files)) {
//
}
But I can not upload multiple files to the similar spring boot controllers:
#RequestMapping(value = "/projects",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
public ResponseEntity<Void> upload(#RequestParam(value="files", required=false) MultipartFile[] files)) {
//
}
#RequestMapping(value = "/projects",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
public ResponseEntity<Void> upload(#RequestParam(value="files", required=false) List<MultipartFile> files)) {
//
}
*The two controllers above have empty list or array when I try to upload files
Do I need some extra configuration in my spring boot to upload multiple files?
Here're my configs for the test project:
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>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>multipart-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>multipart-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
MultipartDemoApplication.java
package com.example.multipartdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MultipartDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MultipartDemoApplication.class, args);
}
}
FileUploadController.java
package com.example.multipartdemo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.stream.Collectors;
#Controller
public class FileUploadController {
#RequestMapping(value = "/projects",
produces = {"application/json"},
consumes = {"multipart/form-data"},
method = RequestMethod.POST)
public ResponseEntity<String> uploadFiles(#RequestParam(value = "files", required = false) MultipartFile[] files) {
try {
String fileNames = Arrays.stream(files)
.map(MultipartFile::getOriginalFilename)
.collect(Collectors.joining(" : "));
return ResponseEntity.status(HttpStatus.OK).body(fileNames);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("Unable to download files");
}
}
#RequestMapping(value = "/projects",
produces = {"text/plain"},
method = RequestMethod.GET)
public ResponseEntity<String> uploadFiles() {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("OK");
}
}
application.properties
spring.servlet.multipart.max-file-size=100KB
spring.servlet.multipart.max-request-size=500KB
After starting application it works as expected:
Only in case of body absence I got the next response, but it's another story of debugging.
So, can you please check your configs? What're the differences with mine?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
i'am building a small APP, with google api and spring boot, and i need to connect mysql db to my project.
First : all my package are in same places (to avoid this kind of issues TT)
So i first try with the doc exemple ( https://spring.io/guides/gs/accessing-data-mysql/) by cloning the project
and its return me : Failed to obtain JDBC Connection.
I try with my own app and i get this :
Error creating bean with name 'entityManagerFactory' defined in + Failed to obtain JDBC Connection too.
application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:8181/testuser
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
dependencies :
<?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 http://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>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.devoteam.presales</groupId>
<artifactId>testspringsecu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testspringsecu</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.27.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>js-cookie</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev493-1.23.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
Entity :
package com.devoteam.presales.testspringsecu;
import javax.persistence.Entity;
#Entity
public class UsersDevo {
private Integer ID;
private String email;
private String nom;
private String prenom;
private String service;
public Integer getId() {
return ID;
}
public void setId(Integer id) {
this.ID = ID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
}
Repository :
package com.devoteam.presales.testspringsecu;
import com.devoteam.presales.testspringsecu.UsersDevo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UsersRepo extends CrudRepository<UsersDevo, Long> {
}
test controller :
package com.devoteam.presales.testspringsecu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
#RequestMapping(path = "/demo")
public class TestController {
#Autowired
UsersRepo usersRepo;
#GetMapping(path = "/all")
public #ResponseBody Iterable<UsersDevo> getAllUsers() {
return usersRepo.findAll();
}
}
main
package com.devoteam.presales.testspringsecu;
import java.security.Principal;
import java.util.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
#SpringBootApplication
#RestController
public class TestspringsecuApplication {
#RequestMapping("/user")
public Principal user(Model model, Principal principal) throws
JSONException {
OAuth2Authentication authentication = (OAuth2Authentication)
principal;
return principal;
}
#GetMapping("/user")
public ModelAndView method() {
System.out.println("icila");
return new ModelAndView("redirect:" + "/");
}
public static void main(String[] args) {
SpringApplication.run(TestspringsecuApplication.class, args);
}
}
I also try a bunch of annotation.
EDIT : i fixe the issue witht the doc exemple with :spring.datasource.url=jdbc:mysql://localhost:8181/testuser?serverTimezone=EST5EDT
but i still have : rg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory'
with my app
I did IT ( after looooog investigation).
And the answer ( for noob like me ) is that your entity must be exactly the same as your table.
my mistake was to forget the #Id
#GeneratedValue(strategy= GenerationType.AUTO) on private Integer ID.
SPring could be painfull some times ... ( often..)
and dont forget ?serverTimezone=EST5EDT after database url.
i hope saving some hours to someone.
I m experimenting with Spring and Rest in order to build and Web app for representing a simple list of the cryptocurrencies.
The controller:
package jasmin.merusic.cryptocurrency.controllers;
import jasmin.merusic.cryptocurrency.services.ApiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class DataController {
private final ApiService apiService;
public DataController(ApiService apiService) {
this.apiService = apiService;
}
#RequestMapping({"", "/", "/index","cryptos"})
public String index(Model model){
model.addAttribute("cryptos",apiService.getCrypto(10));
return "index";
}
}
And then i have the services package where i have the interface for the Api and the implementation for this service right here(where i m overriding a method ):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
#Service
public class ApiServiceImpl implements ApiService{
private RestTemplate restTemplate;
#Autowired
public ApiServiceImpl(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
#Override
public List<Crypto> getCrypto(Integer limit) {
CryptoData crypto = restTemplate.getForObject("https://api.coinmarketcap.com/v2/ticker/?limit=" + limit , CryptoData.class);
return crypto.getCryptos();
}
}
Here is the code of The RestTamplateConfig class
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
#Configuration
public class RestTemplateConfig {
#Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
And here is the class where i m trying to map from the API: `
public class CryptoData {
private List<Crypto> data;
public List<Crypto> getCryptos() {
return data;
}
public void setCryptos(List<Crypto> data) {
this.data = data;
}
}
public class Crypto implements Serializable
{
private Integer id;
private String name;
private String symbol;
private String websiteSlug;
private Integer rank;
private Double circulatingSupply;
private Double totalSupply;
private Double maxSupply;
private Quotes quotes;
private Integer lastUpdated;
private final static long serialVersionUID = 362556439034076810L;
//getters and setter
`
My POM File looks like this:
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jasmin.merusic</groupId>
<artifactId>cryptocurrency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cryptocurrency</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
The problem is that i cannot bind the data to the POJO and i have a error that is
2018-10-19 10:39:35.251 ERROR 14188 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/index]
java.lang.IllegalArgumentException: ConcurrentModel does not support null attribute value
at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.ui.ConcurrentModel.addAttribute(ConcurrentModel.java:75) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.ui.ConcurrentModel.addAttribute(ConcurrentModel.java:39) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at jasmin.merusic.cryptocurrency.controllers.DataController.index(DataController.java:20) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
any help? i m a newvie to this and i m following something in order to this.
You are doing right, but the problem might be due to the lack of a proper setter/getter for the data field in the CryptoData class. By "proper" I mean getData() and setData(List<Crypto>).
If you look closer at the JSON snippet you are receiving, you would notice that the structure is a bit different from yours. It's actually a Map<Integer, Crypto>, not a List<Crypto>.
class CryptoData {
private Map<Integer, Crypto> data;
public Map<Integer, Crypto> getData() {
return data;
}
public void setData(Map<Integer, Crypto> data) {
this.data = data;
}
}
I am building a simple Spring application from the scratch using Maven and PostgreSQL.
I've been following thousand of tutorials but it isn't clear for me where to store the different configurations to connect and work with a PostgreSQL database.
My pom.xml file:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc4</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
My src/main/java/quotes/Application.java file:
package quotes;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
My src/main/java/quotes/Quote.java file:
package quotes;
public class Quote {
private String content;
private String author;
public Quote(String content, String author) {
this.content = content;
this.author = author;
}
public String getContent() {
return this.content;
}
public Quote setContent(String content) {
this.content = content;
return this;
}
public String getAuthor() {
return this.author;
}
public Quote setAuthor(String author) {
this.author = author;
return this;
}
}
And my src/main/java/quotes/QuoteController.java file:
package quotes;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.CrossOrigin;
#Controller
public class QuoteController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
#CrossOrigin(origins = "*")
#RequestMapping("/random")
public #ResponseBody Quote randomQuote() {
return new Quote("¿A dónde vas? Patatas traigo", "Ortega y Pacheco");
}
}
I can build and run the application with Maven:
mvn clean package
java -jar target/*.jar
The idea now is to modify the code on QuoteController.java to connect to PostgreSQL and return a random stored quote.
Could you please give some advices / clues?
You should try this tutorial .
http://devcrumb.com/hibernate/spring-data-jpa-hibernate-maven
You need to add an application.properties file under src/main/resources
It should look like this for PostgreSQL:
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/your-db-name
spring.datasource.username=username
spring.datasource.password=password
Checkout this link for more info:
enter link description here