Spring and RESTfull ConcurrentModel does not support null attribute value - java

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;
}
}

Related

Spring boot 404 not found

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);
}
}

Unable to fetch values from Spring Cloud Config Server to my Config-Client(limits-service)

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>

Uploading multiple files does not work in spring boot controller

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?

Connect with MSSQL with Spring Reactive (R2DBC)

I am currently trying to establish a database connection with a Microsoft SQL Server.
Unfortunately I can not understand why it does not work. And the error message can unfortunately not give me precise information.
It looks like my code isn't even trying to connect to the database.
My Starterclaas:
#SpringBootApplication
public class R2Dbc3Application {
public static void main(String[] args) {
SpringApplication.run(R2Dbc3Application.class, args);
}
}
DatabaseConfiguration:
package com.example.config;
import io.r2dbc.mssql.MssqlConnectionConfiguration;
import io.r2dbc.mssql.MssqlConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
#Configuration
#EnableR2dbcRepositories("com.example.repository")
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
#Value("${spring.data.mssql.host}")
private String host;
#Value("${spring.data.mssql.database}")
private String database;
#Value("${spring.data.mssql.username}")
private String username;
#Value("${spring.data.mssql.password}")
private String password;
#Bean
#Override
public MssqlConnectionFactory connectionFactory() {
System.out.println("Connecting to database" + host);
return new MssqlConnectionFactory(MssqlConnectionConfiguration.builder()
.host(host)
.port(1453)
.database(database)
.username(username)
.password(password)
.build());
}
}
my DatabaseInitializer:
package com.example.config;
import com.example.domain.Person;
import com.example.repository.PersonRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
#Component
public class DatabaseInitializer {
private final Logger log = LoggerFactory.getLogger(DatabaseInitializer.class);
#Autowired
PersonRepository personRepository;
public DatabaseInitializer(PersonRepository personRepository) {
this.personRepository = personRepository;
}
#PostConstruct
public void init() {
log.info("Initializing database if necessary");
personRepository.findAll().count().subscribe(count -> {
if (count == 0) {
log.info("Database is empty, inserting sample data");
createPerson("Josh", "Long", "Pivotal");
createPerson("Julien", "Dubois", "Microsoft");
} else {
log.info("Database is already initialized");
}
});
}
private void createPerson(String firstName, String lastName, String company) {
Person person = new Person();
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompany(company);
personRepository.save(person).log().subscribe();
}
}
Person.Java:
package com.example.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
#Table("person")
public class Person {
#Id
private Long id;
private String firstName;
private String lastName;
private String company;
and getter/setter
My PersonRepository
package com.example.repository;
import com.example.domain.Person;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface PersonRepository extends ReactiveCrudRepository<Person, Long> {
}
And my Controller:
package com.example.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.Person;
import com.example.repository.PersonRepository;
import reactor.core.publisher.Flux;
#RestController
#RequestMapping("/")
public class PersonController {
private final PersonRepository personRepository;
public PersonController(PersonRepository personRepository) {
this.personRepository = personRepository;
}
#GetMapping("/persons")
public Flux<Person> list() {
return personRepository.findAll();
}
}
My Pom:
<?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.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>R2DBC2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>R2DBC2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- see https://github.com/r2dbc/r2dbc-mssql/issues/77 -->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-mssql</artifactId>
<version>1.0.0.M7</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.0.0.gh-151-SNAPSHOT</version>
</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>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
Error Message:
This application has no configured error view, so you are seeing this as a fallback.
Tue May 19 12:23:33 CEST 2020
[58026f55-7] There was an unexpected error (type=Not Found, status=404).
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND
at org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$0(ResourceWebHandler.java:325)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ HTTP GET "/persons" [ExceptionHandlingWebHandler]
Stack trace:
at org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$0(ResourceWebHandler.java:325)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44)
at reactor.core.publisher.Mono.subscribe(Mono.java:4210)
at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onComplete(FluxSwitchIfEmpty.java:75)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onComplete(MonoFlatMap.java:174)
at reactor.core.publisher.MonoNext$NextSubscriber.onComplete(MonoNext.java:96)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:359)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:21
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND
It means the endpoint mapping through #GetMapping was not hit. Remember the paths are joined with defined mappings at the class level and then at the method level, the current endpoint would look like:
localhost:8080//persons
This is incorrect.
As long as the #RestController is a root with no further mapping, don't include the "/" character as long as it adds another "layer" or "subpath" in the path. The correct usage would be #RequestMapping("/endpoint-name"). In your case, you don't want an extra "subpath" so omit the annotation:
#RestController
public class PersonController { ... }
Unfortunatelly, I couldn't find any reference to support my statement.

Cant connect MySQL database to my spring project (despite all post i have read) [closed]

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.

Categories

Resources