我在 RestController 中使用協議 PATCH 的方法有一個小問題,我正在創建一個物件倉庫。
1st 我通過 POST 創建它:
{
"location" : "Paris",
"maxCapacity" : "200"
}
結果:
[
{
"id": 1,
"location": "Paris",
"currentCapacity": 0,
"maxCapacity": 200,
"cargosList": [],
"status": "EMPTY"
}
]
例如,僅修補欄位位置時:
{
"location" : "New York"
}
使用 GET 閱讀時:
[
{
"id": 1,
"location": "New York",
"currentCapacity": 0,
"maxCapacity": null,
"cargosList": [],
"status": "EMPTY"
}
]
盡管我的 PATCH 請求僅在欄位位置上,但為什么欄位 maxCapacity 已更改為 null?
我的物體:
package net.erickcaron.warehouseapplicationapi.models;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.*;
import javax.persistence.*;
import java.util.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "warehouses")
public class Warehouse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String location;
private Integer currentCapacity = 0;
private Integer maxCapacity;
@OneToMany
@JsonManagedReference
private List<Cargo> cargosList = new ArrayList<>();
@Enumerated(value = EnumType.STRING)
private WarehouseStatus status = WarehouseStatus.EMPTY;
}
我的休息控制器:
package net.erickcaron.warehouseapplicationapi.rest;
import net.erickcaron.warehouseapplicationapi.models.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.erickcaron.warehouseapplicationapi.services.WarehouseService;
import javax.transaction.Transactional;
import java.util.List;
@Transactional
@RestController
@RequestMapping("/warehouses")
public class WarehouseRestController {
@Autowired
private WarehouseService warehouseService;
@PostMapping
public Integer create(@RequestBody Warehouse warehouse){
return warehouseService.create(warehouse);
}
@GetMapping("/{id}")
public Warehouse findById(@PathVariable Integer id){
return warehouseService.findById(id);
}
@GetMapping
public List<Warehouse> findAll(){
return warehouseService.findAll();
}
@PatchMapping("/{id}") //TODO not working!
public void partialUpdate(@PathVariable Integer id, @RequestBody Warehouse warehouse){
warehouseService.partialUpdate(id, warehouse);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Integer id){
warehouseService.deleteById(id);
}
@PatchMapping("/{warehouseId}/cargos/{cargoId}")
public void addCargoToWarehouse(@PathVariable Integer warehouseId, @PathVariable Integer cargoId){
warehouseService.addCargoToWarehouse(warehouseId, cargoId);
}
}
感謝您的幫助,問候,埃里克
uj5u.com熱心網友回復:
使用 amapper更新您的物體,然后再次保留它。我不僅可以推薦MapStruct!
您可以這樣創建Mapper:
@Mapper
public interface WarehouseMapper {
// Ignore null Values
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
void updateWarehouse(Warehouse updatedWarehouse, @MappingTarget Warehouse oldWarehouse);
}
然后你可以在partialUpdate()函式中獲取你的映射器并以這種方式使用它:
public void partialUpdate(Integer id, Warehouse updatedWarehouse)
{
Warehouse oldWarehouse = findById(id);
WarehouseMapper mapperInstance = Mappers.getMapper( WarehouseMapper.class );
mapperInstance.updateWarehouse(updatedWarehouse, oldWarehouse); // Maps the NON NULL fields of updatedWarehouse to oldWarehouse fields
warehouseRepository.save(oldWarehouse);
}
或者
自己做映射,不要使用任何外部依賴:
public void partialUpdate(Integer id, Warehouse updatedWarehouse)
{
Warehouse oldWarehouse = findById(id);
if(updatedWarehouse.getLocation() != null)
{
oldWarehouse.setLocation(updatedWarehouse.getLocation());
}
// ... And so on for each field
warehouseRepository.save(oldWarehouse);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351872.html
上一篇:DTO、DAO和物體?是否需要物體?這三個的最佳實踐?
下一篇:CORS策略已阻止從源“http://localhost:62521”訪問“localhost:3000/users”處的XMLHttpRequest
