2011-12-11 3 views
3

함수에서 JSON을 반환하려고하는데 직렬화에 대한 오류가 발생했습니다.jax-RS가 json 데이터를 반환 할 수 없습니다.

오류 :

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.codehaus.jettison.json.JSONArray and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)) 

내가 제대로 반환하지만 난 모르겠어요 확인하기 위해 직렬화와 함께 뭔가를 할 필요가 가정합니다.

package contentmanagement; 

import javax.ws.rs.core.Context; 
import javax.ws.rs.core.UriInfo; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.PUT; 
import javax.ws.rs.Path; 
import javax.ws.rs.GET; 
import javax.ws.rs.Produces; 
import org.codehaus.jettison.json.JSONArray; 

/** 
* REST Web Service 
*/ 
@Path("signups") 
public class ContentManagement { 

    @Context 
    private UriInfo context; 

    /** Creates a new instance of ContentManagement */ 
    public ContentManagement() { 
    } 

    /** 
    * Retrieves representation of an instance of contentmanagement.ContentManagement 
    * @return an instance of java.lang.String 
    */ 
    @GET @Path("getHtml") 
    @Produces("application/json") 
    public JSONArray getHtml() { 
     JSONArray myData = new JSONArray(); 

     for (int x = 0; x < 12; x++) { 
      myData.put("This is a test entry"+x); 
     } 

     return myData; 
    } 
} 

여기에서 잘못 될 수있는 것에 대해 누구보다 통찰력을 줄 수 있습니까?

답변

1

코드에 아무런 문제가 보이지 않습니다. 기본 설정으로 샘플 저지 앱에 넣으면 기본적으로 작동합니다. JSONConfiguration은 어떻게 구성합니까?

getHtml()의 반환 유형을 String으로 변경하고 myData.toString()을 반환 할 수 있습니다. 암시 적 직렬화가 필요하지 않습니다.

2

작성한 코드가 올바른 반면 호스트 프레임 워크에서 지원하는 일련 화 (serialization) 공급자는 표준화되지 않았습니다. 아마도 당신은 JSONArray -> JSON에 등록 된 것이없는 것을 사용하고 있습니까? 그것은 확실히 그렇게 보입니다. 단지 클래스 패스에 내장 된 클래스가 충분 가진, JAX-RS의 일부 프레임 워크 구현에

@Provider 
public class JSONArraySerializer implements MessageBodyWriter<JSONArray> { 
    @Override 
    public boolean isWriteable(Class<?> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType) { 
     // Applicability condition: writing JSONArray to application/json 
     if (JSONArray.class.isAssignableFrom(type)) 
      return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE); 
     return false; 
    } 
    @Override 
    public long getSize(JSONArray array, Class<?> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType) { 
     return -1; // Can't be bothered to calculate 
    } 
    @Override 
    public void writeTo(JSONArray array, Class<?> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType, 
      MultivaluedMap<String, Object> httpHeaders, 
      OutputStream entityStream) throws IOException, 
      WebApplicationException { 
     try { 
      // Strictly should say encoding here; don't know right value... 
      array.write(new OutputStreamWriter(entityStream)); 
     } catch (JSONException e) { 
      throw new WebApplicationException(e); 
     } 
    } 
} 

: 다음은 샘플 제공합니다. 다른 사람들 (특히 Apache CXF, 다른 사람들)에서는 수동으로 등록해야합니다 (동일한 웹 응용 프로그램 내에서 서로 다른 서비스에 대해 서로 다른 일련 화 전략을 사용할 수 있기 때문에 유용하다고 생각했지만 매우 정교한 웹 응용 프로그램을 작성했습니다).

관련 문제