2013-05-14 25 views
14

Yukarıdaki hatayı alıyorum çünkü Jackson'ın POJO'yu seri hale getirme girişimi nedeniyle bir sorun var.Spring REST: HttpMediaTypeNotSupportedException: İçerik türü 'application/json; charset = UTF-8'

Ben kodu debug ettik ve onu Jackson'un ObjectMapper içinde false döndürür:

public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) { 
    JavaType javaType = getJavaType(type, contextClass); 
    return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType)); 
} 

this.objectMapper.canDeserialize (javaType) aşağıdaki gibi

Benim Kontrolörü olan hataya neden olan yanlış döndürür:

@Controller 
public class CancelController { 
    @Autowired 
    private CancelService cancelService; 

    @RequestMapping(value="/thing/cancel", method=RequestMethod.POST, consumes="application/json") 
    public @ResponseBody CancelThingResponseDTO cancelThing(@RequestBody CancelRequestDTO cancelThingRequest) { 
     return cancelService.cancelThing(cancelThingRequest); 
    } 

Benim CancelRequestDTO Serializable uygular:

public class CancelRequestDTO implements Serializable{ 
    /** 
    * Default serialization ID 
    */ 
    private static final long serialVersionUID = 1L; 
    /** 
    * Reason code associated with the request 
    */ 
    private final String reasonCode; 
    /** 
    * Identifier of the entity associated with the request 
    */ 
    private final EntityIdentifier entityIdentifier; 

    /** 
    * Default constructor 
    * 
    * @param reasonCode Reason code associated with the request 
    * @param entityIdentifier Identifier of the entity associated with the request 
    */ 
    public CancelRequestDTO(String reasonCode, EntityIdentifier entityIdentifier) { 
    super(); 
    this.reasonCode = reasonCode; 
    this.entityIdentifier = entityIdentifier; 
    } 
    /** 
    * @return Returns the reasonCode. 
    */ 
    public String getReasonCode() { 
    return reasonCode; 
    } 
    /** 
    * @return Returns the entityIdentifier. 
    */ 
    public EntityIdentifier getEntityIdentifier() { 
    return entityIdentifier; 
    } 
} 

Benim Bahar yapılandırma aşağıdaki gibidir:

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<mvc:annotation-driven /> 

<!-- Scan for stereotype annotations --> 
<context:component-scan base-package="com.cancel.web.controller" /> 

<bean id="viewNameTranslator" 
    class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" /> 

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> 





<bean id="jsonView" 
    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" > 
    <property name="contentType" value="application/json;charset=UTF-8"/> 
    </bean> 

<!-- Register JSON Converter for RESTful Web Service --> 
<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
     <list> 
      <bean 
       class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
      </bean> 
     </list> 
    </property> 
</bean> 

herkes bu seri kaldırma soruna neden olabilir biliyor musun?

Teşekkür

+0

DTO/POJO ürününüzün hatasız olduğu tam kodu nedir? – bh5k

+1

Sadece ekledi, setter yokluğuyla ilgili olabilir mi? – DJ180

cevap

20

DTO'm neden ayarlayıcılı varsayılan yapıcıya sahip değil! Yani hala bu sorunla karşı karşıya, ayrıca üst sınıfında @JsonManagedReference(value ="secondParent") aynı değeri ekleyin tek sınıfta iki @JsonBackReference sahip bu @JsonBackReference(value = "secondParent") gibi referans birine değer katmak olamaz herkes için Jackson

+2

Evet, seri hale getirme ile yapmak, onların istisnaları yanıltıcı olabilir. – bh5k

+2

Benim durumumda, DTO'mn kendi özniteliklerinin bir Harita'sı vardır. Bu özniteliğe @JsonIgnore eklediğimde sorunumu giderdi. – Adam

+0

Benim durumumda 'compiled' özniteliği için' getCompiled' ve 'isCompiled' adlı bir yöntem oluşturuldu ve bu hata Jackson’dan geliyor. –

0

Hep ContentNegotiatingViewResolver kullanarak yapmış. Görünen içerik türünü anlamadığı anlaşılıyor.

:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <property name="order" value="1" /> 
    <property name="contentNegotiationManager"> 
     <bean class="org.springframework.web.accept.ContentNegotiationManager"> 
      <constructor-arg> 
       <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> 
        <constructor-arg> 
         <map> 
          <entry key="json" value="application/json" /> 
          <entry key="xml" value="application/xml" /> 
         </map> 
        </constructor-arg> 
       </bean> 
      </constructor-arg> 
     </bean> 
    </property> 

    <property name="defaultViews"> 
     <list> 
      <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> 
      <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> 
       <constructor-arg> 
        <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> 
         <property name="autodetectAnnotations" value="true" /> 
        </bean> 
       </constructor-arg> 
      </bean> 
     </list> 
    </property> 
</bean> 

Bu video arayüzünde jQuery aracılığıyla hizmete tüketen ile yapmaya çalıştığımız tam olarak ne yaptığını geçer: Bu benim genellikle Yapmaya çalıştığınız şeyleri yaptığım için kullanmak yapılandırmayı olduğu http://pluralsight.com/training/Courses/TableOfContents/springmvc-intro

+0

Yine de, hata, karakter kümesiyle ilgili olsa da, ObjectMapper – DJ180

9

Yanlış bir özel durum gibi görünüyor .

İlgili konular