0
I am beginner in Spring Boot and my application is showing the following error:
{
"timestamp": "2021-07-07T02:27:00.231+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->one.digitalinnovation.personapi.dto.request.PersonDTO[\"job\"]->one.digitalinnovation.personapi.entity.Job$HibernateProxy$040pRItT[\"hibernateLazyInitializer\"])\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:460)\n\tat org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:181)\n\tat org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:124)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:655)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:764)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:228)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\n\tat org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1723)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.base/java.lang.Thread.run(Thread.java:853)\nCaused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->one.digitalinnovation.personapi.dto.request.PersonDTO[\"job\"]->one.digitalinnovation.personapi.entity.Job$HibernateProxy$040pRItT[\"hibernateLazyInitializer\"])\n\tat com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)\n\tat com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1276)\n\tat com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)\n\tat com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)\n\tat com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)\n\tat com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)\n\tat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)\n\tat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)\n\tat com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)\n\tat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)\n\tat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)\n\tat com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)\n\tat com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)\n\tat com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)\n\tat com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)\n\tat com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)\n\tat com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1510)\n\tat com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1006)\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:454)\n\t... 52 more\n",
"message": "Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->one.digitalinnovation.personapi.dto.request.PersonDTO[\"job\"]->one.digitalinnovation.personapi.entity.Job$HibernateProxy$040pRItT[\"hibernateLazyInitializer\"])",
"path": "/api/v1/people"
This error occurs when trying to list registered persons!
I believe it has something to do with recovering the Job object in the Person object.
The project is structured in Entity (Job, Person), Controller(Job, Person), DTO (Job, Person), Service (Job, Person), Mapper - Interface (Job, Person), individually the registered APIS, display all items, search for id.
I decided to create a Job type field in Person and Persondto.
When registration I inform the person object containing the job previously registered by a json, when saved and I will look in the table is recorded correctly, but when I try to recover the person it falls into this error.
Code:
package one.digitalinnovation.personapi.entity;
import com.fasterxml.Jackson.databind.Annotation.Jsonserialize; import Lombok.Allargsconstructor; import Lombok. Builder; import Lombok. Date; import Lombok.Noargsconstructor; import one.digitalinnovation.personapi.mappingjobmapper; import one.digitalinnovation.personapi.service.Jobservice; import org.mapstruct.Mappingtarget;
import javax.persistence. *; import javax.validation.Constraint; import java.time.Localdate; import java.util.List; import java.util.Set;
@Entity @Data @Builder @Allargsconstructor @Noargsconstructor public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
@Column(nullable = false, unique = true)
private String cpf;
private LocalDate birthDate;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE})
private List<Phone> phones;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id_job",nullable=false, foreignKey = @ForeignKey(name="fk_job"))
private Job job;
package one.digitalinnovation.personapi.dto.request request;
import Lombok.Allargsconstructor; import Lombok. Builder; import Lombok. Date; import Lombok.Noargsconstructor; import one.digitalinnovation.personapi.entity.Job; import org.hibernate.Validator.constraints.br.CPF;
import javax.validation.Valid; import javax.validation.constraints.Notempty; import javax.validation.constraints.Notnull; import javax.validation.constraints.Size; import java.util.List;
@Date @Builder @Allargsconstructor @Noargsconstructor public class Persondto {
private Long id;
@NotEmpty
@Size(min = 2, max= 100)
private String firstName;
@NotEmpty
@Size(min = 2, max = 100)
private String lastName;
@NotEmpty
@CPF
private String cpf;
private String birthDate;
@Valid
@NotEmpty
private List<PhoneDTO> phones;
@Valid
@NotNull
private Job job;
}
package one.digitalinnovation.personapi.service;
import Lombok.Allargsconstructor; import one.digitalinnovation.personapi.dto.Messageresponsedto; import one.digitalinnovation.personapi.dto.request.Jobdto; import one.digitalinnovation.personapi.dto.request.Persondto; import one.digitalinnovation.personapi.entity.Job; import one.digitalinnovation.personapi.entity.Person; import one.digitalinnovation.personapi.exception.Personnotfoundexception; import one.digitalinnovation.personapi.mappingjobmapper; import one.digitalinnovation.personapi.mapr.Personmapper; import one.digitalinnovation.personapi.repository.Personrepository; import org.mapstruct.Mapper; import org.springframework.Beans.factory.Annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List; import java.util.stream.Collectors;
@Service @Allargsconstructor(onConstructor = @__(@Autowired)) public class Personservice {
private PersonRepository personRepository;
private final PersonMapper personMapper = PersonMapper.INSTANCE;
public MessageResponseDTO createPerson(PersonDTO personDTO){
Person personToSaved = personMapper.toModel(personDTO);
Person savedPerson = personRepository.save((personToSaved));
return createMessageResponse(savedPerson.getId(), "Create person ID ");
}
public List<PersonDTO> listAll() {
List<Person> allPeople = personRepository.findAll();
return allPeople.stream()
.map(personMapper::toDTO)
.collect(Collectors.toList());
}
public PersonDTO findById(Long id) throws PersonNotFoundException {
Person person = verifyIfExists(id);
return personMapper.toDTO(person);
}
public void delete(Long id) throws PersonNotFoundException {
verifyIfExists(id);
personRepository.deleteById(id);
}
public MessageResponseDTO updateById(Long id, PersonDTO personDTO) throws PersonNotFoundException {
verifyIfExists(id);
Person personToUpdate = personMapper.toModel(personDTO);
Person updatedPerson = personRepository.save(personToUpdate);
return createMessageResponse(updatedPerson.getId(), "Updated person ID ");
}
private Person verifyIfExists(Long id) throws PersonNotFoundException {
return personRepository.findById(id).orElseThrow(() -> new PersonNotFoundException(id));
}
private MessageResponseDTO createMessageResponse(Long id, String message) {
return MessageResponseDTO
.builder()
.message(message + id)
.build();
}
}
package one.digitalinnovation.personapi.map;
import one.digitalinnovation.personapi.dto.request.Persondto; import one.digitalinnovation.personapi.entity.Person; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Factory.Mappers;
@Mapper public interface Personmapper {
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
@Mapping(target = "birthDate", source = "birthDate", dateFormat = "dd-MM-yyyy")
Person toModel(PersonDTO personDTO);
PersonDTO toDTO(Person person);
}
Welcome! Could you post your code please? Also, when posting, put the code in markdown as instructions will appear at the time of editing the question.
– leandro.dev
@leandro.dev Hello. Thank you! I made the changes, but if the code looks better it’s in my Github [link] (https://github.com/rafaelponte89/api-people-coding). The only difference of the code I posted is the Job type field in Person and Persondto and the annotations of these fields.
– Rafael Ponte Cassemiro