【问题标题】:Using JPA Entity to persist HTTP Request使用 JPA 实体持久化 HTTP 请求
【发布时间】:2020-07-06 16:38:26
【问题描述】:

我公开了一个端点,我的客户端调用请求来触发 Spring 批处理作业。

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private MyProcessor processor;

    @PostMapping(value = "/runJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<MyResponse> runJob(@Valid @RequestBody MyRequest request) {
        //persist the request using Spring JPA
        String requestTrackingId = UUID.randomUUID().toString()
        MyResponse response = processor.process(request, requestTrackingId); //run spring batch job and create response
        return new ResponseEntity<>(response, CREATED);
    }

}

请求包括名称和其他一些参数:

public class MyRequest {
    private String name;
    private String runDate;
    private boolean rerun;
}

我需要使用 Spring JPA 将 http 作业运行请求保留在表中。该表应将请求数据与唯一 id 一起保存,以便可以对其进行跟踪,还应捕获作业状态,以便客户端可以使用跟踪 id 查询。

我需要 JPA 实现方面的帮助,包括创建实体并在请求到达端点时将请求持久化到表中。实施会是什么样子?桌子上的id应该是tracking id。

【问题讨论】:

  • 你试过什么?

标签: java spring jpa spring-rest


【解决方案1】:

如果您想要更复杂的答案,请提供更多信息。

一种可能的实现方式:

模型

    @Entity
    @Table
    public class RequestData{
    
        @Id
        @Column(name = "id", nullable = false, unique = true, updatable = false, length = 32)
        private String id; // = IdGenerator.generateId(); #use it if you want to get the id from the object, and not set it.

        @Basic
        private String name;

        @Basic
        private String runDate;

        @Basic
        private Boolean rerun;

        //[getters/setters] here
    
    }

存储库界面

public interface RequestDataRepository extends JpaRepository<RequestData, String>{}

现在您可以将存储库注入您的控制器并保存请求:

    @Autowired
    private RequestDataRepository repository;

    @PostMapping(value = "/runJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<MyResponse> runJob(@Valid @RequestBody MyRequest request) {
        String uniqueId=IdGenerator.generateId()
        RequestData requestData=new RequestData();
        requestData.setId(uniqueId);     
        requestData.setName(request.getName())
        .... //other fields setters (except id)
        repository.save(requestData)
        //alternativelly get the id from the object, then you do not need to create id here, but in the RequestData object
        //String uniqueId=requestData.getId();
        [...]
    }

UUID 生成器

public final class IdGenerator {

        private static org.springframework.util.IdGenerator idGenerator = new AlternativeJdkIdGenerator();
    
        private IdGenerator() {
            throw new UnsupportedOperationException("IdGenerator is an utility class");
        }
    
        public static String generateId() {
            return idGenerator.generateId().toString().replaceAll("-", "");
        }
    }

【讨论】:

  • 你可以使用 UUID.randomUUID() 创建 UUID 并设置它吗?这种方式是为了什么?
  • 来自文档:An IdGenerator that uses SecureRandom for the initial seed and Random thereafter, instead of calling UUID.randomUUID() every time as JdkIdGenerator does. This provides a better balance between securely random ids and performance.
  • 你想要一个唯一的 ID,因为我理解这个问题:request data along with the unique id 。 UUID 以最好地实现这一目标。
  • 因为我在 restcontroller 类中需要它,你能更新使用 UUID 创建的答案并设置为请求对象吗?
  • 你可以从对象中得到它,但我当然会更新给你。
猜你喜欢
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 2011-12-22
  • 2013-02-17
相关资源
最近更新 更多