小编Lit*_*ker的帖子

如何使用Spring Data REST存储库创建和连接相关资源?

我有一个使用Spring Data REST/RestRepository架构的简单概念验证演示.我的两个实体是:

@Entity
@org.hibernate.annotations.Proxy(lazy=false)
@Table(name="Address")
public class Address implements Serializable {

    public Address() {}

    @Column(name="ID", nullable=false, unique=true) 
    @Id 
    @GeneratedValue(generator="CUSTOMER_ADDRESSES_ADDRESS_ID_GENERATOR")    
    @org.hibernate.annotations.GenericGenerator(name="CUSTOMER_ADDRESSES_ADDRESS_ID_GENERATOR", strategy="native")  
    private int ID;

    @RestResource(exported = false)
    @ManyToOne(targetEntity=domain.location.CityStateZip.class, fetch=FetchType.LAZY)   
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.PERSIST}) 
    @JoinColumns({ @JoinColumn(name="CityStateZipID", referencedColumnName="ID", nullable=false) }) 
    private domain.location.CityStateZip cityStateZip;

    @Column(name="StreetNo", nullable=true) 
    private int streetNo;

    @Column(name="StreetName", nullable=false, length=40)   
    private String streetName;

    <setters and getters ommitted>  
}
Run Code Online (Sandbox Code Playgroud)

并为CityStateZip:

@Entity
public class CityStateZip {

    public CityStateZip() {}

    @Column(name="ID", nullable=false, unique=true) 
    @Id 
    @GeneratedValue(generator="CUSTOMER_ADDRESSES_CITYSTATEZIP_ID_GENERATOR")   
    @org.hibernate.annotations.GenericGenerator(name="CUSTOMER_ADDRESSES_CITYSTATEZIP_ID_GENERATOR", strategy="native") 
    private int ID;

    @Column(name="ZipCode", nullable=false, length=10)  
    private …
Run Code Online (Sandbox Code Playgroud)

java rest spring json spring-data-rest

7
推荐指数
1
解决办法
1722
查看次数

如何实现通过 RestTemplate 执行的 PATCH?

我正在编写 JUnit 测试,通过 RestTemplate 调用我的应用程序。我已经成功地实现了 GET、POST 和 PUT,但无法运行 PATCH(尽管它在客户端发送 URL 时工作)。例如,使用以下代码运行 POST:

    RestTemplate restTemplate = new RestTemplate(); 
    ProductModel postModel = restTemplate.postForObject(TestBase.URL + URL, pModel, ProductModel.class);            
Run Code Online (Sandbox Code Playgroud)

但是当我尝试调用 restTemplate.patchForObject() - 我在网上找到的 - STS 返回一个错误,指出该函数未定义。我因此使用了这个:

    RestTemplate restTemplate = new RestTemplate(); 
    ResponseEntity<MessageModel> retval = restTemplate.exchange("http://localhost:8080/products/batchUpdateProductPositions", 
            HttpMethod.PATCH, new HttpEntity<ProductPositionListModel>(pps), MessageModel.class);   
Run Code Online (Sandbox Code Playgroud)

它编译,但给了我一个错误:

I/O Error on PATCH request for "http://localhost:8080/products/batchUpdateProductPositions": Invalid HTTP method: PATCH
Run Code Online (Sandbox Code Playgroud)

在应用程序中,我在 Controller 类中定义了操作:

@RequestMapping(value = "/batchUpdateProductPositions", method = RequestMethod.PATCH)
public MessageModel batchUpdatePosition(
        @RequestBody ProductPositionListModel productPositionList)
        throws Exception {
    try {
        return productService.batchUpdatePosition(productPositionList);
    } …
Run Code Online (Sandbox Code Playgroud)

java rest junit resttemplate http-patch

1
推荐指数
2
解决办法
9384
查看次数

标签 统计

java ×2

rest ×2

http-patch ×1

json ×1

junit ×1

resttemplate ×1

spring ×1

spring-data-rest ×1