在 "error" 中获取 Post 请求结果: "Not Found" 在我的 spring 启动 api
Get and Post request result in "error": "Not Found" on my spring boot api
我正在使用 Spring 数据 JPA 将实体保存到 H2 数据库,get/ post 请求总是 return 错误“未找到”,状态为 404,但是我在 post 请求中发送的数据正在到达数据库并已保存!
我尝试了什么:
确保我的控制器和主要方法在同一个包下。
我的application.properties:
spring.datasource.url=jdbc:h2:./data/myBudget
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
控制器:
package com.example.budgetwithjpa.control;
import com.example.budgetwithjpa.data.ExpenseRepository;
import com.example.budgetwithjpa.entity.Expense;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/budget")
public class ExpenseController {
ExpenseRepository expenseRepo;
@Autowired
public ExpenseController(ExpenseRepository expenseRepo) {
this.expenseRepo = expenseRepo;
}
@GetMapping("/view")
public Iterable<Expense> viewAll(Model model) {
System.out.println(expenseRepo.findAll());
return expenseRepo.findAll();
}
@PostMapping
public String addExpense(@RequestBody Expense expense) {
expenseRepo.save(expense);
return "ok.";
}
}
在上面的代码中,viewAll
方法里面的System.out
会将数据库的内容打印到控制台
存储库:
import com.example.budgetwithjpa.entity.Expense;
import org.springframework.data.repository.CrudRepository;
public interface ExpenseRepository extends CrudRepository<Expense, Long> {
}
我正在向 http://localhost:8080/budget/view 发送获取请求
并得到
{
"timestamp": "2021-08-26T11:10:50.956+00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/budget/view"
}
下面是我的项目文件结构和依赖项的屏幕:
项目文件结构:
项目依赖:
如果您想重现问题,可以从 this github repo 获取应用程序
您缺少 @ResponseBody
注释:
@ResponseBody
@GetMapping("/view")
public Iterable<Expense> viewAll(Model model) {
System.out.println(expenseRepo.findAll());
return expenseRepo.findAll();
}
或者您可以使用 @RestController
而不是 @Controller
。
我正在使用 Spring 数据 JPA 将实体保存到 H2 数据库,get/ post 请求总是 return 错误“未找到”,状态为 404,但是我在 post 请求中发送的数据正在到达数据库并已保存!
我尝试了什么: 确保我的控制器和主要方法在同一个包下。
我的application.properties:
spring.datasource.url=jdbc:h2:./data/myBudget
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
控制器:
package com.example.budgetwithjpa.control;
import com.example.budgetwithjpa.data.ExpenseRepository;
import com.example.budgetwithjpa.entity.Expense;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/budget")
public class ExpenseController {
ExpenseRepository expenseRepo;
@Autowired
public ExpenseController(ExpenseRepository expenseRepo) {
this.expenseRepo = expenseRepo;
}
@GetMapping("/view")
public Iterable<Expense> viewAll(Model model) {
System.out.println(expenseRepo.findAll());
return expenseRepo.findAll();
}
@PostMapping
public String addExpense(@RequestBody Expense expense) {
expenseRepo.save(expense);
return "ok.";
}
}
在上面的代码中,viewAll
方法里面的System.out
会将数据库的内容打印到控制台
存储库:
import com.example.budgetwithjpa.entity.Expense;
import org.springframework.data.repository.CrudRepository;
public interface ExpenseRepository extends CrudRepository<Expense, Long> {
}
我正在向 http://localhost:8080/budget/view 发送获取请求 并得到
{
"timestamp": "2021-08-26T11:10:50.956+00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/budget/view"
}
下面是我的项目文件结构和依赖项的屏幕:
项目文件结构:
项目依赖:
如果您想重现问题,可以从 this github repo 获取应用程序
您缺少 @ResponseBody
注释:
@ResponseBody
@GetMapping("/view")
public Iterable<Expense> viewAll(Model model) {
System.out.println(expenseRepo.findAll());
return expenseRepo.findAll();
}
或者您可以使用 @RestController
而不是 @Controller
。