gpt4 book ai didi

Spring boot Resource Not Found 404错误html

转载 作者:行者123 更新时间:2023-12-05 05:09:45 26 4
gpt4 key购买 nike

我构建了一个名为 Employer 的 crud 应用程序。操作工作正常,但是在 html 控制台中显示它们时显示错误 Not Found

: Resource not found
2019-07-29 05:04:25.834 DEBUG 18656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2019-07-29 05:04:25.834 DEBUG 18656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2019-07-29 05:04:25.834 DEBUG 18656 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-07-29 05:04:25.834 DEBUG 18656 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2019-07-29 05:04:25.834 DEBUG 18656 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Mon Jul 29 05:04:25 PDT 2019, status=404, error=Not Found, message=No message available, (truncated)...]
2019-07-29 05:04:25.835 DEBUG 18656 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
2019-07-29 05:04:37.741 DEBUG 18656 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : GET "/favicon.ico", parameters={}
2019-07-29 05:04:37.742 DEBUG 18656 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []]
2019-07-29 05:04:37.754 DEBUG 18656 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK

我尝试重新启动应用程序并更改实体的名称,但它没有用。以下是我尝试过的

雇主 Controller .java

package io.javabrains;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.mapping.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

import io.javabrains.Entity.Employer;

@Controller
public class EmployerController {


@Autowired
private EmployerService service;




@RequestMapping("/")
public String newForm() {
return "form1";
}






public List<Employer>getAllEmployers()
{


return service.getAllEmployers();



}

@RequestMapping(value="/tables",method=RequestMethod.GET)

public String getAllEmployers(Model model)
{
List<Employer>employers = service.getAllEmployers();
model.addAttribute("Employer",employers);
return "tables";
}





@RequestMapping("/employer/{id}")
public Employer getEmployer(@PathVariable Integer id) {
return service.getEmployers(id);
}



@RequestMapping(method=RequestMethod.POST,value="/employer")
public void addEmployer(@RequestBody Employer employer) {
service.addEmployer(employer);

}


@RequestMapping(method=RequestMethod.PUT,value="/employer/{id}")
public void updateEmployer(@RequestBody Employer employer,@PathVariable int id) {
service.updateEmployer(id,employer);
}




@RequestMapping(method=RequestMethod.DELETE,value="/create/{id}")
public void deleteEmployer(@PathVariable int id)
{
service.deleteEmployer(id);
}




}

雇主服务.java

package io.javabrains;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.javabrains.Entity.Employer;

@Service
public class EmployerService {
@Autowired
private Repository repository;

public List<Employer>getAllEmployers(){
List<Employer>employers = new ArrayList<>();
repository.findAll()
.forEach(employers::add);
return employers;

}

public void addEmployer(Employer employer) {
repository.save(employer);
}


public void updateEmployer(int id, Employer employer) {
repository.save(employer);
}


public void deleteEmployer(int id) {
repository.deleteById(id);
;
}



public Employer getEmployers(int id)
{
return repository.getOne(id);

}





}

雇主.Java

package io.javabrains;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.javabrains.Entity.Employer;

@Service
public class EmployerService {
@Autowired
private Repository repository;

public List<Employer>getAllEmployers(){
List<Employer>employers = new ArrayList<>();
repository.findAll()
.forEach(employers::add);
return employers;

}

public void addEmployer(Employer employer) {
repository.save(employer);
}


public void updateEmployer(int id, Employer employer) {
repository.save(employer);
}


public void deleteEmployer(int id) {
repository.deleteById(id);
;
}



public Employer getEmployers(int id)
{
return repository.getOne(id);

}





}

表格.html

 <tbody>
<tr th:each="$(employers)">
<td th:text="${employers.name}"></td>
<td th:text="${employer.position}"></td>
<td th:text="${employer.office}"></td>
<td th:text="${employer.age}"></td>
<td th:text="${employer.salary}"></td>
</tr>

....

最佳答案

如果您在 Controller 类中使用@Controller (org.springframework.stereotype.Controller),您还必须添加@ResponseBody每个 @RequestMapping 或在类级别。

或者,Spring 引入了 @RestController (org.springframework.stereotype.Controller.RestController),其中包括 @Controller@ResponseBody

此外,您可以在 API 中尝试以下操作,我现在已经测试过并且有效。

@RequestMapping(path="/template")
public ModelAndView getHelloTemplate(){
ModelAndView model = new ModelAndView();
model.setViewName("hello");
model.addObject("message", "Hello World");
return model;
}

我使用了 spring-boot-starter-freemarker 模板引擎,html 文件位于 `src/main/resources/templates/hello.ftl' 下。

现在,我已经用 Thymeleaf 测试了相同的代码并且它有效。

pom.xml:

<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-freemarker</artifactId>-->
<!--<version>2.1.2.RELEASE</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>

Controller :

@Controller(value = "tvGuideController")
public class TvGuideController {
@RequestMapping(path="/template")
public ModelAndView getHelloTemplate(){
ModelAndView model = new ModelAndView();
model.setViewName("hello");
model.addObject("message", "Hello World");
return model;
}

@RequestMapping(path="/template1")
public String getHelloTemplate(Model model){
model.addAttribute("message", "Hello World 1");
return "hello";
}

@RequestMapping(path="/template2")
public String getHelloTemplate(ModelMap model){
model.addAttribute("message", "Hello World 2");
return "hello2";
}
}

同时具有 Thymeleaf 和 Freemarker 模板引擎的运行应用程序可用 at github克隆存储库并运行应用程序:

git clone https://github.com/fiveobjects/reference.git
cd java/springboot
mvn spring-boot:run

URL(端口可能因您而异):

http://localhost:8080/template
http://localhost:8080/template1
http://localhost:8080/template2

如果您想在 pom.xml 中运行 freemarker 而不是 thymeleaf,请注释掉 thymeleaf 依赖项并取消注释 freemarker 依赖项。

如果这对你有用,那么你的 pom.xml 依赖项、application.properties、主应用程序类和 Controller 肯定需要检查。

关于Spring boot Resource Not Found 404错误html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57253641/

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