gpt4 book ai didi

java - Spring Data Rest 字段转换器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:47 30 4
gpt4 key购买 nike

我很难在 spring data REST 项目(无 Controller 应用程序和严格的 java 配置)上使用我的自定义转换器。

我有两个实体,一个雇员和一个州。关系是@ManyToOne,我相信我们都知道。无论如何,问题是将 state 字段(字段名称是 state)从 String 转换为 State 对象和 setState()Employee 类中用于持久化到数据库。

package com.hr.domain;

@Entity
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "state_id", nullable = true)
private Long id;
private String firstname;
private State state;

@StateConverter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_state_id_fk", nullable = false, insertable = true, updatable = true)
private State state;

//GETTERS AND SETTERS
public String getFirstname() {
return firstname();
}

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

public String getState() {
return state();
}

public void setState(State state) {
this.state = state;
}

//HASHCODES AND EQUALS

}


package com.hr.domain;

@Entity
public class State implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "state_id", nullable = true)
private Long id;
private String state_name;

//GETTERS AND SETTERS

//TO STRING
@Override
public String toString() {
return "State [id=" + id + ", state_name=" + state_name + "]";
}

}

我已经在转换服务中注册了我的转换器,但仍然无法在表单提交时将字符串转换为状态对象。

@Bean//(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters(getConverters());
bean.afterPropertiesSet();
ConversionService object = bean.getObject();
System.out.println(object);
return object;
}

private Set<Converter> getConverters() {
Set<Converter> converters = new HashSet<Converter>();
converters.add(new StringToStateTypeConverter());
return converters;
}

这是我的状态转换器

@Component
@StateConverter
public class StringToStateTypeConverter implements Converter<String, State> {

@Autowired
StateRepository repository;

public State convert(String source) {
return repository.findOne(new Long(source));
}

}

提交时,我收到此错误:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB';

我们将不胜感激任何形式的帮助。

愿原力与你同在问候。

这是错误轨迹

17:21:29,975 ERROR [org.springframework.data.rest.webmvc.AbstractRepositoryRestController] (default task-13) Could not read JSON: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"]): org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228) [spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]

...........
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232) [jackson-databind-2.3.3.jar:2.3.3]
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197) [jackson-databind-2.3.3.jar:2.3.3]

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable.
at org.springframework.data.rest.core.UriToEntityConverter.convert(UriToEntityConverter.java:106) [spring-data-rest-core-2.1.0.RELEASE.jar:]
at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:359) [spring-data-rest-webmvc-2.1.0.RELEASE.jar:]

Caused by: java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable.
... 94 more

最佳答案

您正在注册一个新的 ConversionService。它用许多通常假定存在的转换器替换了 spring 默认注册的默认转换器。然后您创建一个新的转换器而不是使用您配置的 bean。你应该改为:

  • 注册spring的默认转换服务及其所有默认转换器
  • 将你的转换器添加到这个

您的转换服务初始化可能如下所示:

private @Autowired StringToStateTypeConverter stringToStateTypeConverter;

@Bean//(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new DefaultFormattingConversionService();
bean.addConverter(String.class, State.class, stringToStateTypeConverter);
return bean;
}

编辑

由于错误来自从 URIState 的转换问题,您可以尝试添加一个从 URI 到 State 的转换器(假设您的存储库有一个方法 findByName 通过 name 查找 State:

@Component
@StateConverter
public class URIToStateTypeConverter implements Converter<URI, State> {
@Autowired
StateRepository repository;

public State convert(URI uri) {
String source = uri.toString();
try {
long id = Long.valueOf(source);
return repository.findOne(id);
} catch (NumberFormatException e) { // should be a name ??
return repository.findByName(source);
}
}
}

转换服务初始化将变成:

private @Autowired StringToStateTypeConverter stringToStateTypeConverter;
private @Autowired URIToStateTypeConverter uriToStateTypeConverter;

@Bean//(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new DefaultFormattingConversionService();
bean.addConverter(String.class, State.class, stringToStateTypeConverter);
bean.addConverter(URI.class, State.class, uriToStateTypeConverter);
return bean;
}

但是我不确定它是否会被spring-data-rest使用

关于java - Spring Data Rest 字段转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781516/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com