繁体   English   中英

不继承Java JAX-RS @PUT批注

[英]Java JAX-RS @PUT annotation is not inherited

我有一个抽象类,它声明一个@PUT方法:

public abstract class BaseResource<T> {

  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public abstract Response create(T entityClass);

}

然后,一个类实现该方法:

public class GroupsResource extends BaseResource<Group> {

  @Override
  public Response create(Group newGroup) {

    // this works.
    // ...
    return response.build();

  }
}

与GET方法类似的代码也可以使用,但是405 Method Not Allowed拒绝了这一代码。 但是,如果我用@PUT注释实现,它确实可以工作。 因此,似乎抽象声明上的@PUT注释没有被继承。

有什么想法吗?

更新:

我从头开始使用Java EE7,这很好用。

我在项目中遇到了同样的问题,我这样解决了它:

//Inject the object mapper because you don't want to retreive an "Object"
@Inject
ObjectMapper objectMapper;

@PUT
//The PUT endpoint retrieves a plain object
public Response update(Object object){
   //Now we determine the concrete type of the object by using reflection
   ParameterizedType p = (ParameterizedType) getClass().getGenericSuperclass();
   Class<DTO> clazz = (Class<DTO>) p.getActualTypeArguments()[0];

   //Pass the object and the determined type to the objectmapper
   DTO dto = objectMapper.convertValue(object, clazz);

   //Now call an abstract method that deals with the object in you concrete class
   updateDto(dto);
}

protected abstract Response updateDto(DTO dto);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM