简体   繁体   中英

cant connect mysql databse using spring boot error HTTP Status 404 the requested resource is not available

Below mentioned code is not fetching data with mysql database using spring boot with maven dependency.After hitting url it was not displaying any json data. Previously I was working with gradle and was able to connect with database and gives me json value. I have copied code from that to maven project, No error in code. Can anyone help me out where I am doing mistake? Please also let me know if any easy way to do the same to make code better as I am naive in this.

1) Running on server

Hitting Url ( http://localhost:8080/CheckListMavenWebThree ) throws error instead of giving json value.

Let you know this is maven Dynamic web project

Error - Displays this on browser HTTP Status 404 the requested resource is not available / Spring boot

HTTP Status 404 - /CheckListMavenWebThree/checklist

type Status report

message /CheckListMavenWebThree/checklist

description The requested resource is not available.

Apache Tomcat/7.0.81

When I am hitting url in Postman for json data (Displays as mentioned below rather than json value from database)

<html>
    <head>
        <title>Apache Tomcat/7.0.81 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>HTTP Status 404 - /CheckListMavenWebThree/</h1>
        <HR size="1" noshade="noshade">
        <p>
            <b>type</b> Status report
        </p>
        <p>
            <b>message</b>
            <u>/CheckListMavenWebThree/</u>
        </p>
        <p>
            <b>description</b>
            <u>The requested resource is not available.</u>
        </p>
        <HR size="1" noshade="noshade">
        <h3>Apache Tomcat/7.0.81</h3>
    </body>
</html>

I am confused with what url should I hit either with project name and path value (mentioned in controller class) like 8080/CheckListMavenWebThree/checklist or 8080/CheckListMavenWebThree like this?

File Structure Looks Like - > Structure: File Structure of my project

@Controller

package com.example.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.MediaType;
import com.example.Entity.Checklist;
import com.example.Service.CheckListService;
@Controller
public class CheckListController {
    @Autowired
    private CheckListService checkListService;

    @RequestMapping(value = "/checklist" , method = RequestMethod.GET)
    @ResponseBody
    public Object index() {
        return checkListService.findAll();
    }

    @RequestMapping(value = "/create", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String create(@RequestBody Checklist checklist) {
        String userSiteName = "";
        try {
            checkListService.save(checklist);

            userSiteName = String.valueOf(checklist.getSite_Name());
            System.out.println(userSiteName);
            System.out.println(checklist.getWSC_Serial_Number());
            System.out.println(checklist.getSetup_Tech());
            System.out.println(checklist.getDate());
            System.out.println(checklist.getCheckList_01());
            System.out.println(checklist.getCheckList_02());
            System.out.println(checklist.getCheckList_03());
            System.out.println(checklist.getCheckList_04());
            System.out.println(checklist.getCheckList_05());
            System.out.println(checklist.getCheckList_06());
            System.out.println(checklist.getCheckList_07());
            System.out.println(checklist.getCheckList_08());
            System.out.println(checklist.getCheckList_09());
            System.out.println(checklist.getCheckList_10());
            System.out.println(checklist.getCheckList_11());
            System.out.println(checklist.getCheckList_12());
            System.out.println(checklist.getCheckList_13());
            System.out.println(checklist.getCheckList_14());
            System.out.println(checklist.getNotes());
            System.out.println(checklist.getCompleted_By());
            System.out.println(checklist.toString());
            System.out.println("----------------------------------------------------");
        }catch(Exception e) {
            return e.toString();
        }
        return userSiteName;
    }
}

@Application Main

package com.example.Main;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableAutoConfiguration
@ComponentScan("com.example")
@EnableJpaRepositories("com.example.Repository")
@SpringBootApplication
@EntityScan(basePackageClasses = com.example.Entity.Checklist.class)
public class SmartRainCheckListApplication {

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

@Entity

package com.example.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "checklist")
public class Checklist {

    public Checklist() {

    }

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

    @Id
    @Column(name = "WSC_Serial_Number")
    private Long WSC_Serial_Number;

    public Checklist(Long wsc_serial_number) {
        this.WSC_Serial_Number = wsc_serial_number;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    // public Checklist(String Site_Name, Long WSC_Serial_Number, String
    // Setup_Tech, String Date, String CheckList_01,
    // String CheckList_02) {
    // this.Site_Name = Site_Name;
    // this.WSC_Serial_Number = WSC_Serial_Number;
    // this.Setup_Tech = Setup_Tech;
    // this.Date = Date;
    // this.CheckList_01 = CheckList_01;
    // this.CheckList_02 = CheckList_02;
    // }

    public Checklist(String site_Name, Long wSC_Serial_Number, String setup_Tech, String date, String checkList_01,
            String checkList_02, String checkList_03, String checkList_04, String checkList_05, String checkList_06,
            String checkList_07, String checkList_08, String checkList_09, String checkList_10, String checkList_11,
            String checkList_12, String checkList_13, String checkList_14, String notes, String completed_By) {
        super();
        Site_Name = site_Name;
        WSC_Serial_Number = wSC_Serial_Number;
        Setup_Tech = setup_Tech;
        Date = date;
        CheckList_01 = checkList_01;
        CheckList_02 = checkList_02;
        CheckList_03 = checkList_03;
        CheckList_04 = checkList_04;
        CheckList_05 = checkList_05;
        CheckList_06 = checkList_06;
        CheckList_07 = checkList_07;
        CheckList_08 = checkList_08;
        CheckList_09 = checkList_09;
        CheckList_10 = checkList_10;
        CheckList_11 = checkList_11;
        CheckList_12 = checkList_12;
        CheckList_13 = checkList_13;
        CheckList_14 = checkList_14;
        Notes = notes;
        Completed_By = completed_By;
    }

    public String getSite_Name() {
        return Site_Name;
    }

    public void setSite_Name(String site_Name) {
        Site_Name = site_Name;
    }

    public Long getWSC_Serial_Number() {
        return 0 + + WSC_Serial_Number;
    } 

    public void setWSC_Serial_Number(Long WSC_Serial_Number) {
        this.WSC_Serial_Number = WSC_Serial_Number;
    }

    public String getSetup_Tech() {
        return Setup_Tech;
    }

    public void setSetup_Tech(String setup_Tech) {
        this.Setup_Tech = setup_Tech;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String date) {
        this.Date = date;
    }

    public String getCheckList_01() {
        return CheckList_01;
    }

    public void setCheckList_01(String CheckList_01) {
        this.CheckList_01 = CheckList_01;
    }

    public String getCheckList_02() {
        return CheckList_02;
    }

    public void setCheckList_02(String CheckList_02) {
        this.CheckList_02 = CheckList_02;
    }

    public String getCheckList_03() {
        return CheckList_03;
    }

    public void setCheckList_03(String checkList_03) {
        CheckList_03 = checkList_03;
    }

    public String getCheckList_04() {
        return CheckList_04;
    }

    public void setCheckList_04(String checkList_04) {
        CheckList_04 = checkList_04;
    }

    public String getCheckList_05() {
        return CheckList_05;
    }

    public void setCheckList_05(String checkList_05) {
        CheckList_05 = checkList_05;
    }

    public String getCheckList_06() {
        return CheckList_06;
    }

    public void setCheckList_06(String checkList_06) {
        CheckList_06 = checkList_06;
    }

    public String getCheckList_07() {
        return CheckList_07;
    }

    public void setCheckList_07(String checkList_07) {
        CheckList_07 = checkList_07;
    }

    public String getCheckList_08() {
        return CheckList_08;
    }

    public void setCheckList_08(String checkList_08) {
        CheckList_08 = checkList_08;
    }

    public String getCheckList_09() {
        return CheckList_09;
    }

    public void setCheckList_09(String checkList_09) {
        CheckList_09 = checkList_09;
    }

    public String getCheckList_10() {
        return CheckList_10;
    }

    public void setCheckList_10(String checkList_10) {
        CheckList_10 = checkList_10;
    }

    public String getCheckList_11() {
        return CheckList_11;
    }

    public void setCheckList_11(String checkList_11) {
        CheckList_11 = checkList_11;
    }

    public String getCheckList_12() {
        return CheckList_12;
    }

    public void setCheckList_12(String checkList_12) {
        CheckList_12 = checkList_12;
    }

    public String getCheckList_13() {
        return CheckList_13;
    }

    public void setCheckList_13(String checkList_13) {
        CheckList_13 = checkList_13;
    }

    public String getCheckList_14() {
        return CheckList_14;
    }

    public void setCheckList_14(String checkList_14) {
        CheckList_14 = checkList_14;
    }

    public String getNotes() {
        return Notes;
    }

    public void setNotes(String notes) {
        Notes = notes;
    }

    public String getCompleted_By() {
        return Completed_By;
    }

    public void setCompleted_By(String completed_By) {
        Completed_By = completed_By;
    }

}

@Application.properties

spring.datasource.url=jdbc:mysql://localhost/smartraindb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

#naming convention according to me
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

@ Pom.xml

<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>CheckListMavenWebThree</groupId>
    <artifactId>CheckListMavenWebThree</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-envers -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-envers</artifactId>
            <version>1.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-envers -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>5.2.10.Final</version>
        </dependency>


    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <testSourceDirectory>src/main/test</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>

                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>7</version>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>
</project>

2) When I tried differently , running as spring boot app it throws error

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)

2017-08-18 12:51:33.066  INFO 56824 --- [           main] c.e.Main.SmartRainCheckListApplication   : Starting SmartRainCheckListApplication on H-H-Temp with PID 56824 (C:\Users\keval.shah\Documents\workspace-sts-3.9.0.RELEASE-new\CheckListMavenWebThree\target\classes started by Keval.Shah in C:\Users\keval.shah\Documents\workspace-sts-3.9.0.RELEASE-new\CheckListMavenWebThree)
2017-08-18 12:51:33.068  INFO 56824 --- [           main] c.e.Main.SmartRainCheckListApplication   : No active profile set, falling back to default profiles: default
2017-08-18 12:51:33.108  INFO 56824 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@448ff1a8: startup date [Fri Aug 18 12:51:33 MDT 2017]; root of context hierarchy
2017-08-18 12:51:33.827  INFO 56824 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2017-08-18 12:51:34.365  WARN 56824 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
2017-08-18 12:51:34.373  INFO 56824 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-18 12:51:34.380 ERROR 56824 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at com.example.Main.SmartRainCheckListApplication.main(SmartRainCheckListApplication.java:19) [classes/:na]
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;)V
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner$Tomcat8TldSkipSetter.setSkipPattern(SkipPatternJarScanner.java:106) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.setPatternToTomcat8SkipFilter(SkipPatternJarScanner.java:61) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.<init>(SkipPatternJarScanner.java:56) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.SkipPatternJarScanner.apply(SkipPatternJarScanner.java:87) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prepareContext(TomcatEmbeddedServletContainerFactory.java:209) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:178) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    ... 8 common frames omitted

Looks like the context path CheckListMavenWebThree is not defined in your properties, so http://localhost:8080/CheckListMavenWebThree/checklist it's going to throw 404. You add to your properties file:

server.contextPath=/CheckListMavenWebThree

or just hit your app like this,

http://localhost:8080/checklist

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM