Hi All,
I am a newbie to Rest framework and Jersey. I got a question about
injecting a resource into a list of abstract class in Jersey Rest. I had
goolged around but didn't find a workaround for it.
I have the following abstract class:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "segment_type")
@JsonSubTypes({
@Type(value = SimpleSegment.class, name = "simple_segment")})
*public abstract class Segment** *
{
public abstract String getName();
public abstract String getValue(Map<String, String> attributeMap);
}
The SimpleSegment class is an implementation of Segment:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
*public class SimpleSegment extends Segment*
{
@JsonProperty
private String value;
// constructor and getters follow.
}
The following is the public interface for posting a list of segment instances:
@Component
@Consumes({ ContentType.CONTENT_TYPE_XML, ContentType.CONTENT_TYPE_JSON })
@Path("/create_segment_list")
@Produces({ ContentType.CONTENT_TYPE_XML, ContentType.CONTENT_TYPE_JSON })
@Scope("singleton")
*public class CreateSegmentListResource** *
{
private xxxManager manager;
@Autowired
public CreateSegmentListResource(final xxxManager manager)
{ this.manager = manager;}
@POST
public Response createAccountingTreatment(
@RequestBody final CreateSegmentListRequest creationRequest,
@Context final HttpHeaders httpHeaders,
@Context final UriInfo uriInfo)
{
System.out.println("Yeah, we will create the segment list!!!");
if (creationRequest == null)
{
System.out.println("it's null!!");
}
else
{
System.out.println("no, it's not null!");
System.out.println("things: " + creationRequest.getList());
}
return Response.status(Status.OK).build();
}
}
The CreateSegmentListRequest is as follows:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
*public class CreateSegmentListRequest** *
{
@JsonProperty
private List<*Segment*> list;
@SuppressWarnings("unused")
private CreateSegmentListRequest() { }
public CreateSegmentListRequest(final List<Segment> list)
{
this.list = list;
}
public List<Segment> getList()
{
return list;
}
}
If I send a post query to the above resource by attaching a json
string of a list of SimpleSegment instances, I get the following
error:
"The request sent by the client was syntactically incorrect (Bad Request)."
However, if the list in CreateSegmentListRequest is changed to a list of
SimpleSegment, then there is no error and the CreateSegmentListRequest can
be correctly constructed in the resource class.
Anyone knows of a workaround for my problem? Obviously I don't want to have
a separate CreateSegmentListRequest for each implementation of Segment
class.
Regards,
Yangyang