NotReadablePropertyException:bean类的无效属性'id'[java.util.LinkedHashMap]

时间:2016-05-28 15:49:12

标签: java spring spring-mvc spring-ioc

我正在尝试在springMVC应用程序中创建一个抽象服务。我的数据流*通过我的图层完美,但我的持久性(抽象),找不到保存/删除/更新我的数据库的方法,这些是类:

abstract class BaseServiceImpl<T, ID extends Serializable>
 implements BaseService<T, ID> {

protected abstract BaseRepository<T, ID> getRepository();

private BaseRepository<T, ID> repository;

public BaseServiceImpl(BaseRepository<T, ID> repository){
   this.repository = repository;
}

public List<T> findAll(){
    return getRepository().findAll();
}

public T find(ID id){
    return getRepository().findOne(id);
}

public T save(T persisted){
    return this.repository.save(persisted);
}

public void delete(ID id){
    getRepository().delete(id);
}

public long count(){
    return getRepository().count();
}

public boolean exists(ID id){
    return getRepository().exists(id);
}  
}

// ---

@Service
public class MedicineServiceImpl
extends BaseServiceImpl<Medicine, Integer> 
implements MedicineService {

@Autowired
private MedicineRepository repository;

@Autowired
public MedicineServiceImpl(MedicineRepository repository) {
    super(repository);
}

@Override
public BaseRepository<Medicine, Integer> getRepository() {
    return repository;
}
}

// -

@MappedSuperclass
abstract class BaseDomain {

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

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}
}

// -

@Entity
@Table(name = "medicines")
public class Medicine
extends BaseDomain implements Serializable{

@NotNull
private String name;
private String description;
private String purpose;
@Column(name = "active_principle")
private String activePrinciple;
@Column(name = "therapeutic_class")
private String therapeuticClass;
@Column(name = "ean_code")
private String eanCode;
@Column(name = "ms_code")
private String msCode;
@Column(name = "avisa_warning")
private String anvisaWarning;
@Column(name = "lab_name")
private String labName;
@Column(name = "lab_phone")
private String labPhone;

@NotNull
private String contraindication;

public Medicine() {
}
}

// -

@NoRepositoryBean
public interface BaseRepository<T,ID extends Serializable>
extends Repository<T,ID> {

List<T> findAll();

long count();

void delete(ID id);

T save(T s);

T findOne(ID id);

boolean exists(ID id);

void delete(T deleted);

}

// -

@Repository
public interface MedicineRepository 
extends BaseRepository<Medicine,    Integer>{

}

我收到500状态错误,我的tocatlog显示以下消息:

  

SEVERE:Servlet [TavalendoDispatcherServlet]的Servlet.service()在路径[/ tavalendo]的上下文中引发异常[请求处理失败;嵌套异常是org.springframework.beans.NotReadablePropertyException:bean类的无效属性'id'[java.util.LinkedHashMap]:在回退访问期间找不到属性的字段!]有根本原因   org.springframework.beans.NotReadablePropertyException:bean类的属性'id'无效[java.util.LinkedHashMap]:在回退访问期间无法找到属性的字段!       在org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.getPropertyValue(DirectFieldAccessFallbackBeanWrapper.java:56)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)       at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:745)

有人能解释发生了什么吗?

4 个答案:

答案 0 :(得分:0)

看来Spring无法找到医学的id领域:

id

实际上,Medicine似乎没有r<-read.csv("/mnt/gcs-bucket/trainS.csv") 字段,因此您需要添加它。

答案 1 :(得分:0)

1)将转换器对象转换为字符串

private String jsonObjectToString( T value ) throws AppException
{

    try
    {
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String strJson = ow.writeValueAsString( value );
        return strJson;
    }
    catch ( JsonProcessingException e )
    {
        LOG.error( e.getMessage(), e );
        throw new AppException( e );
    }

}

2)将转换器字符串转换为对象

@SuppressWarnings( "unchecked" )
private T jsonStringToObject( String strJson, Class<?> clazz ) throws AppException
{

    try
    {
        ObjectMapper mapper = new ObjectMapper();
        T object = ( T ) mapper.readValue( strJson, clazz );
        return object;
    }
    catch ( IOException e )
    {
        LOG.error( e.getMessage(), e );
        throw new AppException( e );
    }

}

3)转换通用T来重置对象引用

private void yourMethod( CommonRq<T> rq ) throws AppException {

   String newStrJson = jsonObjectToString( rq.getData() );
   T newEntity = jsonStringToObject( newStrJson, getEntityClass() );
   T result = this.mantenimientoService.create( newEntity );

}

这对我有用。...

答案 2 :(得分:0)

我最近遇到了类似的问题,并且在尝试创建/更新我的资源时遇到了类似的异常:

Bean类[org.springframework.hateoas.CollectionModel]的无效属性“ id”:在后备访问期间找不到属性的字段!

仔细查看后,由于无法调用API,我似乎使用了错误的HTTP请求标头(Content-Type:text / uri-list而不是Content-Type:application / json)反序列化JSON有效负载。

只是共享以防有人遇到类似的情况,这可能会在调试时为他们节省一些时间

答案 3 :(得分:0)

在JPA中使用Projection时,我遇到了同样的问题。

错误代码:

focusable()

做出以下更改后解决:

Optional<List<AbcProj>> getData(--);