Spring does not see repository during scan on app start - java

Have sandbox springboot app. Decided to try reactive approach, postgres is used as database, I added r2dbc to the project to make my repository reactive. Here is my code:
#SpringBootApplication
public class Springboot2Application {
public static void main(String[] args) {
SpringApplication.run(Springboot2Application.class, args);
}
}
#Repository
public interface ToDoRepository extends
ReactiveCrudRepository<ToDo,String> {
}
#RestController
#RequestMapping("/api/v1")
public class ToDoController {
private final ToDoRepository repository;
public ToDoController(ToDoRepository repository) {
this.repository = repository;
}
#GetMapping(value = "/to-do", produces = {
MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE,
MediaType.TEXT_XML_VALUE})
public ResponseEntity<Flux<ToDo>> getToDos(#RequestHeader
HttpHeaders headers){
return ResponseEntity.ok().body(repository.findAll());
}
}
#Data
#RequiredArgsConstructor
#Entity
#Table(name = "todo")
public class ToDo {
#Id
#NotNull
#GeneratedValue(generator = "system-uuid")
#GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
#NotNull
#NotBlank
private String description;
#CreationTimestamp
private Timestamp created;
#UpdateTimestamp
private Timestamp modified;
private boolean completed;
}
r2dbc config:
#Configuration
#EnableR2dbcRepositories(basePackages =
"com.springboot2.repository")
public class R2DBCConfig extends AbstractR2dbcConfiguration {
#Bean
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get(
ConnectionFactoryOptions.builder()
.option(DRIVER, "postgresql")
.option(HOST, "localhost")
.option(PORT, 5432)
.option(USER, "admin")
.option(PASSWORD, "admin")
.option(DATABASE, "springdb")
.build());
}
}
On application start I'm getting:
Description:
Parameter 0 of constructor in com.springboot2.controller.ToDoController required a bean of type 'com.springboot2.repository.ToDoRepository' that could not be found.
Action:
Consider defining a bean of type 'com.springboot2.repository.ToDoRepository' in your configuration.
I tried to add #ComponentsScan, tried to move ToDoRepository to the root near Springboot2Application, I dont understand why Spring doesn't see repository interface
pom.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 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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<!-- <spring-shell.version>2.1.0</spring-shell.version>-->
</properties>
<dependencies>
<!-- DB,ORM, and plugins-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>0.8.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Reactive libs-->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

My guess the reason is that you have added r2dbc driver to your project, but haven't added spring-boot-starter-data-r2dbc
So, consider this dependency in your pom.xml file (this is the latest version at the time of writing this answer) :
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-r2dbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>3.0.2</version>
</dependency>
EDIT:
Also you can try to use #EnableR2dbcRepositories and specify the package with your repository.
One more possible way to solve this is to extend your repository from R2dbcRepository, instead of ReactiveCrudRepository
This may help when you have multiple spring data reactive modules (r2dbc and something else) being used in the same project. Since ReactiveCrudRepository is a generic interface Spring does not know how to properly configure these beans.

Related

lombok and #Valid not working together spring boot java

I am using lombok for generating Getter, Setter, AllArgsConstructor, NoArgsConstructor for User class and Doing some Validation with the help of #Valid annotation. But for some reason no Validation I put on User Class is working for me.
Below is the User Class
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class User {
#Size(min = 3, max = 15)
#NotBlank
private String username;
#NotNull
private String password;
}
Below is the Controller Class
#RestController
public class ValidationController {
#PostMapping("/validation")
public ResponseEntity<User> validationtest(#Valid
#RequestBody User user) {
return ResponseEntity.ok(user);
}
}
pom file is below
<?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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.validation</groupId>
<artifactId>validation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>validation</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-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Import used
import javax.validation.Valid
import javax.validation.constraints.NotBlank;

Spring Cloud Contract ContractVerifierTest.java not generated

So I'm trying to write contracts in Java, but the problem is that the verifying tests are not running at all. I've tried writing contracts in Groovy and it runs fine, I don't know what's the difference.
Is there some configuration I'm missing for the plugin? I'm following an example project from here.
This is the 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.5.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>contract_producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contract_producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
<spring-cloud.version>2020.0.5</spring-cloud.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>mysql</groupId>
<artifactId>mysql-connector-java</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.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</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.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>2.2.8.RELEASE</version>
<extensions>true</extensions>
<configuration>
<testFramework>JUNIT5</testFramework>
<baseClassForTests>
com.example.contract_producer.contracts.BaseTest
</baseClassForTests>
<contractsDirectory>src/test/java/contracts</contractsDirectory>
<depedencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>${spring-cloud-contract.version}</version>
<scope>compile</scope>
</dependency>
</depedencies>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
This is the contract written in Java
package contracts;
import org.springframework.cloud.contract.spec.Contract;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class UserControllerContracts implements Supplier<Contract> {
#Override
public Contract get() {
return Contract.make(new Consumer<Contract>() {
#Override
public void accept(Contract c) {
c.name("Get All User Contract");
c.description("Contract for /users and /user/all");
c.request(request -> {
request.method(request.GET());
request.url("/user/all");
});
c.response(response -> {
response.status(response.OK());
response.body("[{\"id\":1,\"email\":\"user#email.com\",\"password\":\"password\",\"name\":\"User\"}]");
});
}
});
}
public static class User {
Integer id;
String email;
String password;
String name;
public User(Integer id, String email, String password, String name) {
this.id = id;
this.email = email;
this.password = password;
this.name = name;
}
}
}
This is the contract written in Groovy
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
description 'Should return all user in database'
request {
method GET()
url '/user/all'
}
response {
status OK()
headers {
contentType applicationJson()
}
body '''
[{
"id": 1,
"email": "user#email.com",
"password": "password",
"name": "User"
}]
'''
}
}
You're using wrong version of the plugin. Look that you're using contract in version 3.0.x through the 2020.0.x release train plugin. Since the old plugin generates junit4 tests the latest boot doesn't run it. Just upgrade the plugin to the latest 3.0.x version.

Facing o.s.web.servlet.PageNotFound : No mapping for GET / problem in my spring boot project

I am trying to implement REST microservices using Spring Boot in my project. I have created a controller for login page and also created repository.
But while hitting URL I am facing this issue o.s.web.servlet.PageNotFound: No mapping for GET /
Main Class
#ComponentScan(basePackages = {"com.springboot.controller.repository"})
#EntityScan(basePackages="com.springboot.controller.model")
#EnableJpaRepositories(basePackages="com.springboot.controller.repository")
#SpringBootApplication
public class CunsultustodayWebServicesApplication {
public static void main(String[] args) {
SpringApplication.run(CunsultustodayWebServicesApplication.class, args);
}
}
Controller
#RestController
#ComponentScan
#Controller
public class LoginController {
#Autowired(required=true)
private UserRepo userrepo;
#RequestMapping("/")
public String checkMVC()
{
return "Login";
}
#RequestMapping("/login")
public String loginHome(#RequestParam("email") String email, #RequestParam("password") String password, Model model)
{
User u = null;
try {
u= userrepo.findByEmail(email);
}
catch(Exception ex) {
System.out.println("User Not Found!!!");
}
if(u!=null) {
model.addAttribute("email", email);
return "HomePage";
}
return "Login";
}
}
Repository
#Service("UserRepo")
public interface UserRepo extends JpaRepository<User,Integer> {
User findByEmail(String email);
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.controller</groupId>
<artifactId>cunsultustoday-web-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cunsultustoday-web-services</name>
<description>Demo project for Spring Boot</description>
<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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Please help me to find solution. Thank You
Your controller is probably not being picked up by the ComponentScan because you have limited to the package for the scan.
Try this:
// note #ComponentScan is removed as it is included already in #SpringBootApplication
#EnableJpaRepositories
#SpringBootApplication
public class CunsultustodayWebServicesApplication {
...
And This:
// note #Controller and #CompnentScan are removed
#RestController
public class LoginController {

MockMvc returned HttpMessageNotWritableException with application/json

I have a rest endpoint with spring 2.3.4.RELEASE
When I run test for controller with MockMvc I received
w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.http.converter.HttpMessageNotWritableException:
No converter for [class com.example.myexample.model.User] with preset
Content-Type 'application/json']
#SpringBootTest(classes = UserController.class)
#ExtendWith(SpringExtension.class)
#AutoConfigureMockMvc
public class UserControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private UserRepository userRepository;
private static final String GET_USERBYID_URL = "/users/{userId}";
#Test
public void shouldGetUserWhenValid() throws Exception {
Address address = new Address();
address.setStreet("1 Abc St.");
address.setCity("Paris");
User userMock = new User();
userMock.setFirstname("Lary");
userMock.setLastname("Pat");
userMock.setAddress(address);
when(userRepository.findById(1)).thenReturn(Optional.of(userMock));
mockMvc.perform(get(GET_USERBYID_URL, "1").accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
}
#RestController
#RequestMapping(path = "/users")
#Slf4j
public class UserController {
#Autowired
private UserRepository userRepository;
#GetMapping(value = "/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(#PathVariable("userId") Integer userId) {
log.info("Getting user with id={}", userId);
final Optional<User> userOptional = userRepository.findById(userId)
return userOptional.map(value -> ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(value))
.orElseGet(() -> ResponseEntity.noContent()
.build()).ok()
.body(userOptional.get());
}
}
#Data
public class User {
private String firstname;
private String lastname;
private Address address;
}
#Data
public class Address {
private String street;
private String city;
}
#Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
I've read other article that recommended to added jackson dependency, but this doesn't work for me.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.3</version>
</dependency>
This is my 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 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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>myexample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<lombok.version>1.18.12</lombok.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-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.3</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If I run the application and test it through postman, the endpoint works fine.
Any ideas why I get this error message on my rest controller test?
The problem lies in your #SpringBootTest, when you added classes = UserController.class , it tells spring to create a context with UserController.class. So it create spring context only with UserController. As a result only your controller bean is created and other beans needed for Http Message Converting is not created. So one solution is removing classes = UserController.class from #SpringBootTest.
#SpringBootTest
#ExtendWith(SpringExtension.class)
#AutoConfigureMockMvc
Now you should have no problem running the tests.
But this comes with another problem, now this will load the full spring context which is not needed for only Controller Layer Testing. It will also increase the time of your unit tests.
To solve this problem , spring has another annotation #WebMvcTest . If you annotate yous tests with #WebMvcTest, Spring Boot instantiates only the web layer rather than the whole context. You can also choose to instantiate only one controller. For example,
#ExtendWith(SpringExtension.class)
#WebMvcTest(UserController.class)
public class UserControllerTest {
}
This should solve your problem.
Apparently I put the UserControllerTest in a wrong package name.
It's working now after I put the UserControllerTest in the same package name as the UserController.

JPA is not executing any query

I'm Stuck with JPA since two days , im trying to save a new Object to database but i got no luck with it, it's just throw no exception so i don't know what is going on. why my code is not working
here is the controller
#RestController
public class RealEstatesViewController {
#Autowired
private RealEstateRepository realEstatesViewRepository;
#RequestMapping(value = "/realEstatesView", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<String> addRealEstatesClass(#RequestBody ArrayList<RealEstatesView> realEstatesViews)
throws Exception {
realEstatesViewRepository.save(realEstatesViews);
return new ResponseEntity<>(HttpStatus.OK);
}
}
here is my Model i implemented my columns with my tables
#Entity
#Table(name = "real_estates_views")
public class RealEstatesView {
#GeneratedValue
#Id
#Column(name = "real_estate_view_id")
private int realEstateViewId;
#Column(name = "real_estate_offer_id")
private int realEstateOfferId;
#Column(name = "real_estate_view_date")
private String realEstateViewDate;
#Column(name = "real_estate_view_time")
private String realEstateViewTime;
}
here is my repo
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.akarat.demo.model.RealEstatesView;
#Repository
public interface RealEstateRepository extends CrudRepository<RealEstatesView, Integer> {
}
and my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>AkaratDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AkaratDemo</name>
<description>Demo project for Akarat</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-docs</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.8-dmr</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
finally the resource folder
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3307/demo?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Categories