无法使用 JpaRepository 解析 SpringMVC 中的 ResponseEntity
Cannot resolve ResponseEntity in SpringMVC using JpaRepository
这是我的控制器:
package com.hodor.booking.controller;
import com.hodor.booking.jpa.domain.Vehicle;
import com.hodor.booking.service.VehicleService;
import com.wordnik.swagger.annotations.Api;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/api/v1/vehicles")
@Api(value = "vehicles", description = "Vehicle resource endpoint")
public class VehicleController {
private static final Logger log = LoggerFactory.getLogger(VehicleController.class);
@Autowired
private VehicleService vehicleService;
@RequestMapping(method = RequestMethod.GET)
public List<Vehicle> index() {
log.debug("Getting all vehicles");
return vehicleService.findAll();
}
@RequestMapping(value="/save", method=RequestMethod.POST, consumes="application/json")
@ResponseBody
public Vehicle setVehicle(@RequestBody Vehicle vehicle) {
log.debug("Inserting vehicle");
if (vehicle.getLicensePlate() == null){
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
return vehicleService.saveVehicle(vehicle);
}
}
我在上面 If-Guard 中想要实现的是,如果车辆 Object 没有 LicensePlate 成员,则发回相应的 HTTP 状态 Header CONFLICT 或其他内容.
我来自 Node 和 Express 背景,我习惯于设置我的 header,发送响应并完成它。但是在这种情况下(JPA)它似乎不起作用。有什么想法吗?
一种替代方法是利用 Spring 的验证支持来声明性地添加 POJO 验证。基本上,您向 Vehicle
class 添加注释,例如:
public class Vehicle {
@NotNull
private LicensePlate licensePlate;
// getters, setters
}
然后您向控制器方法添加 @Valid
注释:
@ResponseBody
public Vehicle setVehicle(@RequestBody @Valid Vehicle vehicle) {
log.debug("Inserting vehicle");
return vehicleService.saveVehicle(vehicle);
}
如果验证失败,Spring 将 return 返回 400 响应。
确保您的 class 路径上有 JSR-303/JSR-349 Bean Validation 实现,例如 Hibernate Validator(它可以在没有 Hibernate 的 ORM 支持的情况下使用)。
可以在 Spring 参考文档的 validation chapter 中找到更多信息。
您使用的 Spring MVC 是哪个版本?来自另一个 post 。它声明 Spring MVC 4.1 及更高版本使用不同的语法。
这是我的控制器:
package com.hodor.booking.controller;
import com.hodor.booking.jpa.domain.Vehicle;
import com.hodor.booking.service.VehicleService;
import com.wordnik.swagger.annotations.Api;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/api/v1/vehicles")
@Api(value = "vehicles", description = "Vehicle resource endpoint")
public class VehicleController {
private static final Logger log = LoggerFactory.getLogger(VehicleController.class);
@Autowired
private VehicleService vehicleService;
@RequestMapping(method = RequestMethod.GET)
public List<Vehicle> index() {
log.debug("Getting all vehicles");
return vehicleService.findAll();
}
@RequestMapping(value="/save", method=RequestMethod.POST, consumes="application/json")
@ResponseBody
public Vehicle setVehicle(@RequestBody Vehicle vehicle) {
log.debug("Inserting vehicle");
if (vehicle.getLicensePlate() == null){
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
return vehicleService.saveVehicle(vehicle);
}
}
我在上面 If-Guard 中想要实现的是,如果车辆 Object 没有 LicensePlate 成员,则发回相应的 HTTP 状态 Header CONFLICT 或其他内容.
我来自 Node 和 Express 背景,我习惯于设置我的 header,发送响应并完成它。但是在这种情况下(JPA)它似乎不起作用。有什么想法吗?
一种替代方法是利用 Spring 的验证支持来声明性地添加 POJO 验证。基本上,您向 Vehicle
class 添加注释,例如:
public class Vehicle {
@NotNull
private LicensePlate licensePlate;
// getters, setters
}
然后您向控制器方法添加 @Valid
注释:
@ResponseBody
public Vehicle setVehicle(@RequestBody @Valid Vehicle vehicle) {
log.debug("Inserting vehicle");
return vehicleService.saveVehicle(vehicle);
}
如果验证失败,Spring 将 return 返回 400 响应。
确保您的 class 路径上有 JSR-303/JSR-349 Bean Validation 实现,例如 Hibernate Validator(它可以在没有 Hibernate 的 ORM 支持的情况下使用)。
可以在 Spring 参考文档的 validation chapter 中找到更多信息。
您使用的 Spring MVC 是哪个版本?来自另一个 post 。它声明 Spring MVC 4.1 及更高版本使用不同的语法。