spring boot怎么写具体get我只能用一个例子来描述
Posted
技术标签:
【中文标题】spring boot怎么写具体get我只能用一个例子来描述【英文标题】:Spring boot how to write a specific get i can only describe with an example 【发布时间】:2021-09-02 18:11:04 【问题描述】:The database I'm talking about你好,我想创建一个 GET 端点,例如返回每个项目的 route_short_name 仅哪个 route_long_name = Autobuz。我的实体文件看起来像这样,我正在使用 Jpa 存储库,我不知道该怎么办,我还有一个服务和一个控制器类,我正在尝试实现这一切。
package stpt.entities;
import javax.persistence.*;
@Entity
@Table(name = "routes")
public class Routes
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "route_id")
private String routeId;
@Column(name = "route_short_name")
private String routeShortName;
@Column(name = "route_long_name")
private String routeLongName;
public Routes()
public String getRouteId()
return this.routeId;
public String getRouteShortName()
return this.routeShortName;
public String getRouteLongName()
return this.routeLongName;
public void setRouteId(String routeId)
this.routeId = routeId;
public void setRouteShortName(String routeShortName)
this.routeShortName = routeShortName;
public void setRouteLongName(String routeLongName)
this.routeLongName = routeLongName;
public boolean equals(final Object o)
if (o == this) return true;
if (!(o instanceof Routes)) return false;
final Routes other = (Routes) o;
if (!other.canEqual((Object) this)) return false;
final Object this$routeId = this.getRouteId();
final Object other$routeId = other.getRouteId();
if (this$routeId == null ? other$routeId != null : !this$routeId.equals(other$routeId)) return false;
final Object this$routeShortName = this.getRouteShortName();
final Object other$routeShortName = other.getRouteShortName();
if (this$routeShortName == null ? other$routeShortName != null : !this$routeShortName.equals(other$routeShortName))
return false;
final Object this$routeLongName = this.getRouteLongName();
final Object other$routeLongName = other.getRouteLongName();
if (this$routeLongName == null ? other$routeLongName != null : !this$routeLongName.equals(other$routeLongName))
return false;
return true;
protected boolean canEqual(final Object other)
return other instanceof Routes;
public int hashCode()
final int PRIME = 59;
int result = 1;
final Object $routeId = this.getRouteId();
result = result * PRIME + ($routeId == null ? 43 : $routeId.hashCode());
final Object $routeShortName = this.getRouteShortName();
result = result * PRIME + ($routeShortName == null ? 43 : $routeShortName.hashCode());
final Object $routeLongName = this.getRouteLongName();
result = result * PRIME + ($routeLongName == null ? 43 : $routeLongName.hashCode());
return result;
public String toString()
return "Routes(routeId=" + this.getRouteId() + ", routeShortName=" + this.getRouteShortName() + ", routeLongName=" + this.getRouteLongName() + ")";
【问题讨论】:
【参考方案1】:检查这个简单的例子。希望能帮到你。
// creating controller to receive requests
@RestController
@RequestMapping("/route")
public class RouteController
@Autowired // it's not a best practice to call repository on controllers. this only to example
private RouteRepository routeRepository;
@GetMapping("/way1/routeLongName")
public ResponseEntity<List<Routes>> getByLongName(@PathVariable("routeLongName") String routeLongName)
return new ResponseEntity<List<Routes>>( routeRepository.listRoutesByLongName(routeLongName) , HttpStatus.OK );
@GetMapping("/way2") // this the best way for api design in your case
public ResponseEntity<List<Routes>> getByLongNameOtherWay(Routes route)
return new ResponseEntity<List<Routes>>( routeRepository.listRoutesByLongName(route.getRouteLongName()) , HttpStatus.OK );
// creating repositoty/dao to read database
@Repository
public interface RouteRepository extends JpaRepository<Routes,String>
// using jpql to describe a select
@Query("select obj from ##entityName obj where routeLongName = ?1 ")
List<Routes> listRoutesByLongName(String routeLongName);
别忘了在你的 pom.xml 中导入 spring 数据依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
运行springboot应用程序后调用resquest
http://localhost:8080/route/way1/Autobuz
http://localhost:8080/route/way2?routeLongName=Autobuz
【讨论】:
以上是关于spring boot怎么写具体get我只能用一个例子来描述的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot 中 WebMvcConfigurationSupport 详解
spring boot中三个jsp页面之间怎么传递参数?第一个页面传值,第三个页面怎么获取参数?<%%>获取不到参数