gpt4 book ai didi

java - 无法使用spring boot连接mysql数据库错误HTTP Status 404请求的资源不可用

转载 作者:行者123 更新时间:2023-11-29 18:30:04 24 4
gpt4 key购买 nike

下面提到的代码没有使用 spring boot 和 maven 依赖项来使用 mysql 数据库获取数据。点击 url 后,它没有显示任何 json 数据。以前我使用 gradle 并能够连接数据库并给我 json 值。我已将代码从该代码复制到 Maven 项目,代码中没有错误。谁能帮我解决我做错的地方?也请让我知道是否有任何简单的方法可以做同样的事情来使代码更好,因为我对此很天真。

1) 在服务器上运行

点击 Url ( http://localhost:8080/CheckListMavenWebThree ) 会抛出错误,而不是提供 json 值。

让你知道这是maven动态web项目

错误 - 在浏览器上显示此内容HTTP Status 404 请求的资源不可用/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

当我在 Postman 中点击 url 获取 json 数据时(显示如下所述,而不是数据库中的 json 值)

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

我很困惑应该使用项目名称和路径值(在 Controller 类中提到)点击哪个网址,例如 8080/CheckListMavenWebThree/checklist8080/CheckListMavenWebThree 等这个?

文件结构看起来像 ->结构: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;
}
}

@应用程序主

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

@实体

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) 当我尝试不同的方式时,作为 Spring Boot 应用程序运行它抛出错误

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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

最佳答案

看起来上下文路径 CheckListMavenWebThree 未在您的属性中定义,因此 http://localhost:8080/CheckListMavenWebThree/checklist 它将抛出 404。您添加到您的属性文件:

server.contextPath=/CheckListMavenWebThree

或者像这样点击你的应用程序,

http://localhost:8080/checklist

希望这有帮助

关于java - 无法使用spring boot连接mysql数据库错误HTTP Status 404请求的资源不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com