No table is being created neither the RestController Url is working

依然范特西╮ 提交于 2019-12-14 02:23:33

问题


please tell what I am doing wrong, the code is running with no errors but I am getting no tables or the working restcontroller url whatsoever

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.usere.entity.UserEntity;

@RestController
@RequestMapping("/usr")
public class Ucontroller {


@RequestMapping("/showall")
public UserEntity showall()
{
    return new UserEntity("abc",29);
}
}

pojo or model for rest

import javax.persistence.*;

@Entity
@Table(name = "usrtbl")
public class UserEntity {


@Id
@GeneratedValue
private int uid;


@Column(name = "name")
private String usrname;

@Column(name = "age")
private int age;

public UserEntity(String string, int i) {
    // TODO Auto-generated constructor stub
    usrname=string;
    age=i;
}

//getter setters omitted
}

the autogenerated class @SpringBootApplication public class UserEnittyApplication {

public static void main(String[] args) {
    SpringApplication.run(UserEnittyApplication.class, args);
}

}

servlet initializer(auto generated) public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
    return application.sources(UserEnittyApplication.class);
}

}

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 
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.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.user</groupId>
<artifactId>UserEnitty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>UserEnitty</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-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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

----------------------------EOF------------------

application.properties

server.port=7777

spring.datasource.url = jdbc:mysql://localhost:3306/poncho
spring.datasource.username = root
spring.datasource.password = password

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver




spring.jpa.properties.hibernate.dialect =         
org.hibernate.dialect.MySQL5Dialect

spring.jpa.generate-ddl=true


spring.jpa.hibernate.ddl-auto = update

spring.mvc.view.prefix=/view/    // I tried using a simple controller 
spring.mvc.view.suffix=.jsp      // it didn't work too. 

console

2019-08-25 10:41:06.534  INFO 9608 --- [           main] com.usere.demo.UserEnittyApplication     : No active profile set, falling back to default profiles: default
2019-08-25 10:41:08.997  INFO 9608 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-08-25 10:41:09.055  INFO 9608 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 37ms. Found 0 repository interfaces.
2019-08-25 10:41:09.925  INFO 9608 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d899bdb3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-08-25 10:41:10.761  INFO 9608 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7777 (http)
2019-08-25 10:41:10.840  INFO 9608 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-25 10:41:10.841  INFO 9608 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-08-25 10:41:11.205  INFO 9608 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-25 10:41:11.206  INFO 9608 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4551 ms
2019-08-25 10:41:11.653  INFO 9608 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-08-25 10:41:12.147  INFO 9608 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-08-25 10:41:12.328  INFO 9608 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-08-25 10:41:12.533  INFO 9608 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.10.Final}
2019-08-25 10:41:12.537  INFO 9608 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-08-25 10:41:12.938  INFO 9608 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-08-25 10:41:13.309  INFO 9608 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-08-25 10:41:13.874  INFO 9608 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-08-25 10:41:14.769  INFO 9608 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-25 10:41:14.954  WARN 9608 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-08-25 10:41:15.610  INFO 9608 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7777 (http) with context path ''
2019-08-25 10:41:15.618  INFO 9608 --- [           main] com.usere.demo.UserEnittyApplication     : Started UserEnittyApplication in 9.922 seconds (JVM running for 13.859)```



回答1:


Create a repository class and call it for example UserEntityRepository like this:

public interface UserEntityRepository extends JpaRepository<UserEntity, Integer> {
}

Now create a service class and call it UserEntityService like this:

public interface PlayerService {

    List<UserEntity> findAll();

    Player findById(int theId);

    void save(UserEntity theUserEntity);

    void deleteById(int theId);    
}

You don't necessarily need all these methods but these are common used crud methods.

Then you need to create a class that implements your service class for example:

@Service
public class UserEntityServiceImpl implements UserEntityService {

    private UserEntityRepository userEntityRepository;

    @Autowired
    public CompanyBlueprintServiceImpl(UserEntityRepository theUserEntityRepository ) {
        userEntityRepository= theUserEntityRepository ;
    }

...
    @Override
    public void save(UserEntity theUserEntity) {
        userEntityRepository.save(theUserEntity);
    }
}

After doing all this you have to use your UserEntityService in your controller class. So you can call the save() method from your service to save your entity in your db.

@RestController
@RequestMapping("/api")
public class Ucontroller {

private UserEntityService userEntityService;

public Ucontroller(UserEntityService theUserEntityService) {
userEntityService = theUserEntityService;
}

@RequestMapping("/showall")
public UserEntity showall()
{
    return new UserEntity("abc",29);
}

@PostMapping("/users")
    public UserEntity addUser(@RequestBody UserEntity theUserEntity) {

        // just in case an id in JSON was pass ... set id to 0
        // this is to force a save of new item ... instead of update
        theUserEntity.setUid(0);

        userEntityService.save(theUserEntity);

        return theCompany;
    }

}

What you did is just creating a UserEntity object and returning it in your controller method showAll(). After saving the userEntity you should see the row in your db.




回答2:


I am sorry to bother but I did a great mistake, the project package wasn't named correctlyenter image description here

you see the default generated package is com.usere.demo, earlier I named the other packages as com.usere.controller/entity which was wrong and we shouldn't do that, I thought it might not be important to post the package name, that said the correct name should be com.usere.demo.[anything you desire], thank you Constantin Beer and BaDr Amer for helping instantly and telling the right convention and workflow but it was a very stupid mistake that I made.



来源:https://stackoverflow.com/questions/57643603/no-table-is-being-created-neither-the-restcontroller-url-is-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!