这是一个简单的代码,我从youtube上得到的实践.好吧,我做了正确的代码,但我不能得到明确的Map正确,这是什么404错误的 Postman 说.我不知道这是我的Spring Boot 应用程序的问题或代码错误.请看看这个,因为Spring启动没有给出一个正确的答案.
//控制器
@Slf4j
@RestController
@RequestMapping("/api/v1")
public class RestuController {
@Autowired
private RestuService restuService;
/////////////////////////////////////POST MAPPING//////////////////////////////////////////////////////////////////////
@PostMapping("/POST/save")
Restaurant addRestaurant(@RequestBody Restaurant restaurant) {
return restuService.saveRestu(restaurant);
}
@PostMapping("/POST/saves")
List<Restaurant> addRestaurants(@RequestBody List<Restaurant> restaurants) {
return restuService.saveRestaurants(restaurants);
}
@GetMapping("/GET/resturants")
List<Restaurant> findAllRestaurants(){
log.debug("reached /GET/resturants");
return restuService.getRestaurants();
}
@GetMapping("/GET/{id}")
Restaurant findRestaurantById(@PathVariable int id) {
log.debug("reached /GET/{id} :" + id);
return restuService.getRestaurantById(id);
}
@GetMapping("/GET/{name}")
Restaurant findRestaurantByName(@PathVariable String name) {
log.debug("reached /GET/{name}:" + name);
return restuService.getRestaurantByname(name);
}
@GetMapping("/GET/{dist}")
List<Restaurant> findRestaurantByDist(@PathVariable int distance) {
log.debug("reached /GET/{dist} :" + distance);
return restuService.getRestaurantbyDist(distance);
}
@GetMapping("/GET/{loc}")
List<Restaurant> findRestaurantByLocation(@PathVariable String location) {
log.debug("reached /GET/{loc} : " + location);
return restuService.getRestaurantbyLocation(location);
}
@GetMapping("/GET/{cuisine}")
List<Restaurant> findRestaurantByCuisine(@PathVariable String cuisine) {
log.debug("reached /GET/{cuisine} : " + cuisine);
return restuService.getRestaurantbyCuisine(cuisine);
}
@GetMapping("/GET/{budget}")
List<Restaurant> findRestaurantByBudget(@PathVariable int budget) {
log.debug("reached /GET/{Budget} : " + budget);
return restuService.getRestaurantbyBudget(budget);
}
}
//服务
package SERVICE;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ENTITY.Restaurant;
import REPOSITORY.RestuRepository;
@Service
public class RestuService {
@Autowired
RestuRepository restuRepository;
///////////////////postmethods//////////////////////////////////////
public Restaurant saveRestu(Restaurant restaurant) {
return restuRepository.save(restaurant);
}
public List<Restaurant> saveRestaurants(List<Restaurant> restaurants) {
return restuRepository.saveAll(restaurants);
}
///////////////////////////////////////////////////////////////////////////////////////
//////////////////GET METHODS//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
public List<Restaurant> getRestaurants(){
return restuRepository.findAll();
}
public Restaurant getRestaurantById(int id){
return restuRepository.findById(id).orElse(null);
}
public Restaurant getRestaurantByname(String name){
return restuRepository.findByName(name);
}
public List<Restaurant> getRestaurantbyDist(int distance){
return restuRepository.findByDist(distance);
}
public List<Restaurant> getRestaurantbyLocation(String location){
return restuRepository.findByLocation(location);
}
public List<Restaurant> getRestaurantbyCuisine(String cuisine){
return restuRepository.findBycuisine(cuisine);
}
public List<Restaurant> getRestaurantbyBudget(int budget){
return restuRepository.findBybudget(budget);
}
}
//储存库
package REPOSITORY;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import ENTITY.Restaurant;
@Repository
public interface RestuRepository extends JpaRepository<Restaurant, Integer> {
Restaurant findByName(String name);
List<Restaurant> findByDist(int distance);
List<Restaurant> findByLocation(String location);
List<Restaurant> findBycuisine(String cuisine);
List<Restaurant> findBybudget(int budget);
}
//实体或模型类
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "restaurant_tbl")
public class Restaurant {
@Id
@GeneratedValue
private int id;
private String name;
private String cuisine;
private String location;
private int distance;
private int budget;
}
//APPLICATION.PROPERTIES
server.port = 6282
spring.datasource.dbcp2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url =jdbc:mysql://localhost:3306/restaurants
spring.datasource.username = root
spring.datasource.password = ****
spring.jpa.show-sql =true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
//POM.XML
<?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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sangamam</groupId>
<artifactId>RESTAURANT-SERVICE</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RESTAURANT-SERVICE</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.4</version>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
1条答案
按热度按时间eoxn13cs1#
看到上面的代码看起来一切都很好,你已经添加了swagger依赖关系,并在数据库中配置,所以,而不是 Postman 只是运行程序,并在swagger用户界面测试它,这样你就可以得到正确的答案