[SPRING] Spring Rest - 예외 Json 데이터 생성하기 [duplicate]
SPRINGSpring Rest - 예외 Json 데이터 생성하기 [duplicate]
json rest call을 통해 공개하고자하는 값 객체가 있습니다. 내 프로젝트에는 여러 가지 다른 나머지 호출이 있지만 잘 작동하지만이 1은 어떤 이유로 든하지 않습니다. 이 객체를 다시 반환하려고하면 예외 처리가 어떻게되는지 잘 모릅니다.
값 객체 코드는 다음과 같습니다 (접근 자 메소드 제외).
package com.test.domain.dao;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "company")
public class CompanyVO extends BaseDAOVO implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4278014816235716721L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "companyName", unique = true, nullable = false, length = 200)
private String companyName;
@Column(name = "companyLogo", unique = true, nullable = false, length = 10000)
private String companyLogo;
@Column(name = "isDefaultCompany",nullable = false)
private boolean isDefaultCompany;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
@Cascade({ CascadeType.DELETE, CascadeType.SAVE_UPDATE })
@JsonIgnore
private Set<UserVO> users = new HashSet<UserVO>(0);
}
json을 통해 회사 객체를 반환하는 내 휴식 방법은 다음과 같습니다.
/**
* Method to get the company for a particular userId
* @param userId
* @return
*/
@RequestMapping(value = { GET_COMPANY_FOR_USER }, method = RequestMethod.GET)
public CompanyVO getCompanyForUser(
@PathVariable(value = RestConstants.USER_ID_PLACEHOLDER) Integer userId) {
LOG.info("Entered getCompanyForUser method - userId:{}", userId);
CompanyVO companyVO = companyServices.getCompanyForUser(userId);
LOG.info("Exiting getCompanyForUser method");
return companyVO;
}
이 특정 rest 메서드를 호출하면 stacktrace에서 다음과 같은 오류가 발생합니다.
[2015-03-02 11:12:30,229] [http-bio-8080-exec-8] [INFO] com.test.webapp.controllers.rest.CompanyRestController - Entered getCompanyForUser method - userId:1
[2015-03-02 11:12:30,248] [http-bio-8080-exec-8] [INFO] com.test.webapp.controllers.rest.CompanyRestController - Exiting getCompanyForUser method
[2015-03-02 11:12:30,267] [http-bio-8080-exec-8] [INFO] c.t.webapp.controllers.exceptions.GlobalExceptionHandlerController - Entered exception method: e:{}
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.test.domain.dao.CompanyVO_$$_jvstb99_6["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.test.domain.dao.CompanyVO_$$_jvstb99_6["handler"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:238) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:161) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:202) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) [catalina.jar:7.0.54]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.54]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat7-websocket.jar:7.0.54]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.54]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.54]
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150) [spring-orm-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.54]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.54]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:186) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.54]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.54]
at org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:118) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.54]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.54]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.54]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.54]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [catalina.jar:7.0.54]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.54]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [catalina.jar:7.0.54]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) [catalina.jar:7.0.54]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.54]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.54]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) [tomcat-coyote.jar:7.0.54]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) [tomcat-coyote.jar:7.0.54]
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) [tomcat-coyote.jar:7.0.54]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_40]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_40]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.54]
at java.lang.Thread.run(Thread.java:724) [na:1.7.0_40]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.test.domain.dao.CompanyVO_$$_jvstb99_6["handler"])
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59) ~[jackson-databind-2.4.3.jar:2.4.3]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26) ~[jackson-databind-2.4.3.jar:2.4.3]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:505) ~[jackson-databind-2.4.3.jar:2.4.3]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639) ~[jackson-databind-2.4.3.jar:2.4.3]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) ~[jackson-databind-2.4.3.jar:2.4.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:114) ~[jackson-databind-2.4.3.jar:2.4.3]
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1887) ~[jackson-databind-2.4.3.jar:2.4.3]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:231) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
... 50 common frames omitted
이 정렬에 어떤 도움을 많이 주시면 감사하겠습니다.
해결법
-
==============================
1.이미 발견 한 솔루션 외에도 두 가지 솔루션을 더 고려할 수 있습니다. Hibernate4Module을 ObjectMapper에 등록하는 것은 HibernateProxySerializer를 등록 할 것이고, 이미 프록시를 다루는 인텔리전스를 갖추고 있기 때문에 이해하기 어려운 코드를 확인할 수 있습니다. 등록은 또한 아주 똑바로 앞으로 그들의 docs를 형성한다 :
이미 발견 한 솔루션 외에도 두 가지 솔루션을 더 고려할 수 있습니다. Hibernate4Module을 ObjectMapper에 등록하는 것은 HibernateProxySerializer를 등록 할 것이고, 이미 프록시를 다루는 인텔리전스를 갖추고 있기 때문에 이해하기 어려운 코드를 확인할 수 있습니다. 등록은 또한 아주 똑바로 앞으로 그들의 docs를 형성한다 :
public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { registerModule(new Hibernate4Module()); } }
등록보다
<mvc:annotation-driven> <mvc:message-converters> <!-- Use the HibernateAware mapper instead of the default --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="path.to.your.HibernateAwareObjectMapper" /> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
또 다른 해결책은 DTO 객체를 사용하고 엔티티에서 값을 복사 할 때 보일러 플레이트 코드를 추가하지만 실제 엔티티에서 표현을 더 잘 제어하고 분리 할 수 있습니다
-
==============================
2.masterSlave에 따르면 링크 및 솔루션을 제공하기 때문에 Spring Boot를 채택했습니다. 내 Spring Boot 프로젝트에서 문제를 직면하고 HibernateAwareObjectMapper를 생성하고 아래처럼 맞춤형 새 MappingJackson2HttpMessageConverter를 정의하도록 해결한다.
masterSlave에 따르면 링크 및 솔루션을 제공하기 때문에 Spring Boot를 채택했습니다. 내 Spring Boot 프로젝트에서 문제를 직면하고 HibernateAwareObjectMapper를 생성하고 아래처럼 맞춤형 새 MappingJackson2HttpMessageConverter를 정의하도록 해결한다.
먼저 pom.xml에 hibernate 4에 대한 의존성을 추가한다.
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate4</artifactId> <version>2.5.3</version> </dependency>
HibernateAwareObjectMapper 클래스 만들기
/** * https://github.com/FasterXML/jackson-datatype-hibernate * */ public class HibernateAwareObjectMapper extends ObjectMapper { /** * */ private static final long serialVersionUID = 1L; public HibernateAwareObjectMapper() { registerModule(new Hibernate4Module()); } }
Application.java에 사용자 정의 MappingJackson2HttpMessageConverter 클래스 추가
@Bean public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); HibernateAwareObjectMapper hibernateAwareObjectMapper = new HibernateAwareObjectMapper(); jsonConverter.setObjectMapper(hibernateAwareObjectMapper); return jsonConverter; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(customJackson2HttpMessageConverter()); super.addDefaultHttpMessageConverters(converters); }
-
==============================
3.답변이 포함 된 링크를 제공 한 Evgeni에게 감사드립니다. 다음 주석을 사용하여 CompanyVO bean에 주석을 추가하면 오류가 제거되었습니다. @JsonIgnoreProperties ({ "hibernateLazyInitializer", "handler"})
답변이 포함 된 링크를 제공 한 Evgeni에게 감사드립니다. 다음 주석을 사용하여 CompanyVO bean에 주석을 추가하면 오류가 제거되었습니다. @JsonIgnoreProperties ({ "hibernateLazyInitializer", "handler"})
from https://stackoverflow.com/questions/28808412/spring-rest-exception-producing-json-data by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] Spring WebApp에서 Elasticsearch 클라이언트를 사용할 때의 링크 오류 (0) | 2019.05.01 |
---|---|
[SPRING] Spring 데이터 JPA는 하나의 복합 키만 자동으로 증가시키는 문제입니다. (0) | 2019.05.01 |
[SPRING] CommandLineRunner 실행 실패 - 스프링 배치 (0) | 2019.05.01 |
[SPRING] 값을 집계하고 단일 값을 쓰는 스프링 배치 (0) | 2019.05.01 |
[SPRING] SpelEvaluationException 메서드를 찾을 수 없습니다. (0) | 2019.05.01 |