简体   繁体   English

防止spring-data-rest解析json

[英]Prevent spring-data-rest from parsing json

I would like to store a post request as a string in a database using spring-data-rest and spring-data-jpa. 我想使用spring-data-rest和spring-data-jpa将post请求作为字符串存储在数据库中。 The problem is that spring tries to convert the json request body to an object. 问题是spring尝试将json请求体转换为对象。 Can I prevent this from happening? 我可以防止这种情况发生吗? Can I configure spring to save the request body as a String? 我可以配置spring以将请求体保存为String吗?

Below is the entity and the repository. 下面是实体和存储库。

Entity 实体

@Entity
public class DatabaseRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String json;

    public DatabaseRecord(String json) {
        this.json = json;
    }

    public Long getId() {
        return id;
    }

    public String getJson() {
        return json;
    }
}

Repository 知识库

public interface ExampleRepository extends CrudRepository<DatabaseRecord, Integer> {

    @Override
    @RestResource(exported = false)
    void delete(Integer integer);

    @Override
    @RestResource(exported = false)
    void delete(DatabaseRecord record);

    @Override
    @RestResource(exported = false)
    void delete(Iterable<? extends DatabaseRecord> iterable);

    @Override
    @RestResource(exported = false)
    void deleteAll();
}

Any help would be much appreciated. 任何帮助将非常感激。

Thanks. 谢谢。

The problem is that spring tries to convert the json request body to an object. 问题是spring尝试将json请求体转换为对象。

It's main idea of Spring Data REST - translating JSON directly into an instance for ORM mechanism to persist. 它是Spring Data REST的主要思想 - 将JSON直接转换为ORM机制持久化的实例。 If it was possible, where would this raw data be saved? 如果可能,这个原始数据将保存在哪里?

Can I prevent this from happening? 我可以防止这种情况发生吗?

No, you can't (at least, via Spring Data REST module). 不,你不能(至少通过Spring Data REST模块)。

Can spring I configure spring to save the request body as a string? 可以spring我配置spring将请求体保存为字符串吗?

You could use a plain Spring MVC controller which will delegate a request to REST repositories but before it does some processing raw data (eg saving it into another table). 您可以使用一个普通的Spring MVC控制器,它将请求委托给REST存储库,但在它处理原始数据之前(例如将其保存到另一个表中)。


The line @RestResource(exported = false) means excluding the method from the API, it doesn't effect on the method behaviour (if you tried to use it for such purposes). @RestResource(exported = false)表示从API中排除方法,它不会影响方法行为(如果您尝试将其用于此类目的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM