小编Fre*_*red的帖子

为Spring Validator实现编写JUnit测试

我正在使用Spring Validator实现来验证我的对象,我想知道你如何为这样的验证器编写单元测试:

public class CustomerValidator implements Validator {

private final Validator addressValidator;

public CustomerValidator(Validator addressValidator) {
    if (addressValidator == null) {
        throw new IllegalArgumentException(
          "The supplied [Validator] is required and must not be null.");
    }
    if (!addressValidator.supports(Address.class)) {
        throw new IllegalArgumentException(
          "The supplied [Validator] must support the validation of [Address] instances.");
    }
    this.addressValidator = addressValidator;
}

/**
* This Validator validates Customer instances, and any subclasses of Customer too
*/
public boolean supports(Class clazz) {
    return Customer.class.isAssignableFrom(clazz);
} …
Run Code Online (Sandbox Code Playgroud)

validation junit spring mocking

21
推荐指数
1
解决办法
3万
查看次数

使用 Jackson 解析 XML 时如何忽略特定节点

我想知道在使用 Jackson ML 模块解析 XML 时是否可以忽略一个或多个节点。

我希望能够解析这个 XML

<bundle>
  <id value="myBundleId"/>
  <meta>
    <profile value="http://myurl/profile1" />
    <profile value="http://myurl/profile2" />
    <tag>
      <system value="https://myurl/system" />
      <code value="myAppCode"/>
    </tag>
  </meta>
  <type value="message" />
</bundle>
Run Code Online (Sandbox Code Playgroud)

进入这个 POJO 对象

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import lombok.Data;

@Data
public class Bundle {

    @JacksonXmlElementWrapper(localName = "id")
    @JacksonXmlProperty(isAttribute = true, localName = "value")
    private String id;

    @JacksonXmlElementWrapper(localName = "type")
    @JacksonXmlProperty(isAttribute = true, localName = "value")
    private String type;
}
Run Code Online (Sandbox Code Playgroud)

现在它不起作用,因为我认为注释 @JacksonXmlElementWrapper 仅适用于列表。

它还给了我以下错误消息:

java.lang.IllegalArgumentException:属性“value”的 setter 定义冲突

java xml jackson jackson-dataformat-xml

6
推荐指数
2
解决办法
5169
查看次数

Spring Data REST - 如何同时保存父实体和子实体

我正在使用 Spring Data REST 来公开我的实体,并且我希望能够同时保存(创建和更新)父实体及其子实体。

这是我的实体:

@Entity
@Table(name = "scenario")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Scenario extends AbstractAuditableEntity {

    @Id
    // Sequence name must be preceded by schema name.
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "scenarioIdSeq")
    @SequenceGenerator(name = "scenarioIdSeq", sequenceName = "scenario_id_seq", allocationSize = 1)
    @Column(unique = true, nullable = false, columnDefinition = "SERIAL")
    private Long id;

    @Version
    // Used by JPA for optimistic locking
    protected int version;

    @OneToMany(mappedBy = "scenario")
    @LazyCollection(LazyCollectionOption.TRUE)
    private Set<Action> actions = new HashSet<Action>(); …
Run Code Online (Sandbox Code Playgroud)

rest spring transactions spring-data spring-data-rest

5
推荐指数
1
解决办法
1893
查看次数

Spring LDAP - 如何管理编码(SHA)密码

我想使用Spring LDAP实现一个基本的用户存储库,它是对象目录映射(ODM)的概念.

我的User类非常简单:

@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "shadowAccount", "top" }, base = "ou=people")
public class User {
    [...]

    @Id
    private Name dn;

    @Attribute(name = "uid")
    @DnAttribute(value = "uid")
    private String username;

    @Attribute(name = "cn")
    private String fullName;

    @Attribute(name = "givenName")
    private String firstName;

    @Attribute(name = "sn")
    private String lastName;

    @Attribute(name = "o")
    private String organization;

    @Attribute(name = "userPassword")
    private String password;

    // Getters & Setters
    [...]
}
Run Code Online (Sandbox Code Playgroud)

和我的存储库的基本方法:

public User findByUid(String uid) {
    return ldapTemplate.findOne(query().where("uid").is(uid), User.class);
}

public …
Run Code Online (Sandbox Code Playgroud)

spring ldap change-password sha spring-ldap

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