Java RestController REST 参数依赖注入或请求 [英] Java RestController REST parameter dependency injection or request

查看:83
本文介绍了Java RestController REST 参数依赖注入或请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注入依赖项或至少过滤进入 Spring 中 RestControler 的 ID 参数.我在春天很新.我如何确保 API 中传递的参数有效和/或如何注入与客户实体相关的依赖项?

I am trying inject a dependency or at least filtrate the ID parameter that come into a RestControler in Spring. I am very new in Spring. How can i be sure that the parameter that comes passed in the API is valid and/or how can i inject its dependency related to Customer Entity?

这是我的剩余控制器 CustomerController 方法

This is my rest controller CustomerController method

@PatchMapping("/{id}")
    public Customer updateCustomer(@PathVariable Long id, @RequestBody Customer customer) {
        return customerService.updateCustomer(id, customer);
    }

这是目前只过滤名字和姓氏的请求

This is the request that at the moment filtrates only the firstname and last name

package com.appsdeveloperblock.app.ws.requests.customer;
import javax.validation.constraints.NotNull;

public class CreateCustomerRequest {


    @NotNull
    private String firstname;

    @NotNull
    private String lastname;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }


}

谢谢!

推荐答案

您需要 Bean Validation API(您可能已经拥有)及其参考实现(例如 hibernate-validator).在此处查看 Java Bean 验证基础

You need the Bean Validation API (which you probably already have) and it's reference implementation (e.g. hibernate-validator). Check here Java Bean Validation Basics

总结

  1. 将相应的依赖项添加到您的 pom.xml(或 gradle):

<dependencies>
  <dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
  </dependency>

  <dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.1.2.Final</version>
  </dependency>

  <dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator-annotation-processor</artifactId>
    <version>6.1.2.Final</version>
  </dependency>
</dependencies>

  1. 在您的 Customer 实体上使用 @Valid 注释以自动验证有效负载:
  1. Use @Valid annotation on your Customer entity to have the payload validated automatically:

@PatchMapping("/{id}")
public Customer updateCustomer(@PathVariable Long id, @RequestBody @Valid Customer customer) {
  return customerService.updateCustomer(id, customer);
}

  1. 您可以使用更多注释来装饰 CustomerCreateCustomerRequest 类的字段,例如@Size@Max@Email 等.查看教程以获取更多信息.
  1. You can decorate the fields of your Customer or CreateCustomerRequest class with further annotations, e.g. @Size, @Max, @Email etc. Check the tutorial for more information.

这篇关于Java RestController REST 参数依赖注入或请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆