• <i id='P1DFc'><tr id='P1DFc'><dt id='P1DFc'><q id='P1DFc'><span id='P1DFc'><b id='P1DFc'><form id='P1DFc'><ins id='P1DFc'></ins><ul id='P1DFc'></ul><sub id='P1DFc'></sub></form><legend id='P1DFc'></legend><bdo id='P1DFc'><pre id='P1DFc'><center id='P1DFc'></center></pre></bdo></b><th id='P1DFc'></th></span></q></dt></tr></i><div id='P1DFc'><tfoot id='P1DFc'></tfoot><dl id='P1DFc'><fieldset id='P1DFc'></fieldset></dl></div>
      <bdo id='P1DFc'></bdo><ul id='P1DFc'></ul>
  • <legend id='P1DFc'><style id='P1DFc'><dir id='P1DFc'><q id='P1DFc'></q></dir></style></legend>

  • <tfoot id='P1DFc'></tfoot>
    1. <small id='P1DFc'></small><noframes id='P1DFc'>

        使用 JAX-RS 继承

        时间:2023-09-27
          <i id='9uOo4'><tr id='9uOo4'><dt id='9uOo4'><q id='9uOo4'><span id='9uOo4'><b id='9uOo4'><form id='9uOo4'><ins id='9uOo4'></ins><ul id='9uOo4'></ul><sub id='9uOo4'></sub></form><legend id='9uOo4'></legend><bdo id='9uOo4'><pre id='9uOo4'><center id='9uOo4'></center></pre></bdo></b><th id='9uOo4'></th></span></q></dt></tr></i><div id='9uOo4'><tfoot id='9uOo4'></tfoot><dl id='9uOo4'><fieldset id='9uOo4'></fieldset></dl></div>

              <tbody id='9uOo4'></tbody>
              <bdo id='9uOo4'></bdo><ul id='9uOo4'></ul>
              <tfoot id='9uOo4'></tfoot>
            • <legend id='9uOo4'><style id='9uOo4'><dir id='9uOo4'><q id='9uOo4'></q></dir></style></legend>

                • <small id='9uOo4'></small><noframes id='9uOo4'>

                  本文介绍了使用 JAX-RS 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在为我的网络服务使用 JAX-RS.我有共同的功能,想使用继承.我提供简单的 CRUD 操作.我已经定义了一个这样的接口:

                  I am using JAX-RS for my web services. I have common functionality and would like to use inheritance. I am providing simple CRUD operations. I have defined an interface like so:

                  public interface ICRUD {
                  
                      @POST
                      @Consumes("application/json")
                      @Produces("application/json")
                      @Path("create")
                      public String createREST(String transferObject);
                  
                      @GET
                      @Consumes("application/json")
                      @Produces("application/json")
                      @Path("retrieve/{id}")
                      public String retrieveREST(@PathParam("id") String id);
                  
                      @POST
                      @Consumes("application/json")
                      @Produces("application/json")
                      @Path("update")
                      public void updateREST(@Suspended final AsyncResponse asyncResponse,
                                             final String transferObject) ;
                  
                      @DELETE
                      @Consumes("application/json")
                      @Produces("application/json")
                      @Path("delete/{id}")
                      public String deleteREST(@PathParam("id") String id); 
                  }
                  

                  我有一个实现这个接口的抽象类:

                  I have an abstract class that implements this interface:

                  public abstract class BaseREST implements ICRUD{
                  
                  private final ExecutorService executorService = Executors.newCachedThreadPool();
                  
                  @Override
                  public String createREST(String transferObject) {
                      return create(transferObject).toJson();
                  }
                  
                  @Override
                  public String retreiveREST(@PathParam("id") String id) {
                      return retreive(id).toJson();
                  }
                  
                  
                  @Override
                  public String deleteREST(
                          @PathParam("id") String id) {
                      return delete(id).toJson();
                  }
                  
                  @Override
                      public void updateREST(@Suspended final AsyncResponse asyncResponse, final String transferObject) {
                          executorService.submit(new Runnable() {
                              @Override
                              public void run() {
                                  asyncResponse.resume(doUpdateREST(transferObject));
                              }
                          });
                      }      
                  
                  }
                  

                  最后,我的实现类只是为资源提供了一个 PATH:

                  And lastly, my implementing class simply provides a PATH for the resource:

                  @Path("meeting")
                  public class MeetingRestServices extends BaseREST {
                  }
                  

                  当我尝试访问我的资源时(假设上下文根是/):

                  When I try to access my resource at (assuming the context root is /):

                  http://localhost:8080/webresources/meeting/retreive/0
                  

                  我得到一个 404,它说它找不到它.我的想法是,在继承的某个地方,它弄乱了我认为资源应该在哪里的路径.对此有什么想法吗?

                  I get a 404, it says it can not find it. My thoughts are that somewhere in the inheritance, it is messing with the path of where I think the resource should be. Any thoughts on this?

                  webresources 定义如下.此类由 Netbeans 自动添加.

                  webresources is defined below. This class is added automatically by Netbeans.

                  @javax.ws.rs.ApplicationPath("webresources")
                  public class ApplicationConfig extends Application {
                  
                      @Override
                      public Set<Class<?>> getClasses() {
                          Set<Class<?>> resources = new java.util.HashSet<>();
                          addRestResourceClasses(resources);
                          return resources;
                      }
                  
                      /**
                       * Do not modify addRestResourceClasses() method.
                       * It is automatically populated with
                       * all resources defined in the project.
                       * If required, comment out calling this method in getClasses().
                       */
                      private void addRestResourceClasses(Set<Class<?>> resources) {
                          resources.add(com.dv.meetmefor.ws.impl.BinaryDataRestService.class);
                          resources.add(com.dv.meetmefor.ws.impl.ImageRestServices.class);
                          resources.add(com.dv.meetmefor.ws.impl.LocaleRestService.class);
                          resources.add(com.dv.meetmefor.ws.impl.MeetUpRestServices.class);
                          resources.add(com.dv.meetmefor.ws.impl.MeetingRestServices.class);
                          resources.add(com.dv.meetmefor.ws.impl.UserAccountRestServices.class);
                      }
                  
                  }
                  

                  推荐答案

                  你上面描述的看起来不错.以下是 JAX-RS 继承规则,这些规则基于您提供的内容.

                  What you've described above looks good. Here are the rules for JAX-RS inheritance which based on what you've provided you are adhering.

                  来自 JAX-RS 规范 §3.6:

                  JAX-RS 注释可以用于超类或已实现的方法和方法参数界面.此类注解由相应的子类或实现类继承方法,前提是该方法及其参数没有任何自己的 JAX-RS 注释.注释超类上的优先级高于已实现接口上的优先级.如果一个子类或实现方法具有任何 JAX-RS 注释,然后是超类或接口方法上的所有注释被忽略.例如:

                  JAX-RS annotations MAY be used on the methods and method parameters of a super-class or an implemented interface. Such annotations are inherited by a corresponding sub-class or implementation class method provided that method and its parameters do not have any JAX-RS annotations of its own. Annotations on a super-class take precedence over those on an implemented interface. If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the super class or interface method are ignored. E.g.:

                  public interface ReadOnlyAtomFeed {
                      @GET @Produces("application/atom+xml")
                      Feed getFeed();
                  }
                  
                  @Path("feed")
                  public class ActivityLog implements ReadOnlyAtomFeed {
                      public Feed getFeed() {...}
                  }
                  

                  在上面,ActivityLog.getFeed从接口继承了@GET@Produces注解.反之:

                  In the above, ActivityLog.getFeed inherits the @GET and @Produces annotations from the interface. Conversely:

                  @Path("feed")
                  public class ActivityLog implements ReadOnlyAtomFeed {
                      @Produces("application/atom+xml")
                      public Feed getFeed() {...}
                  }
                  

                  在上面,ReadOnlyAtomFeed.getFeed上的@GET注解不是被ActivityLog继承的.getFeed

                  In the above, the @GET annotation on ReadOnlyAtomFeed.getFeed is not inherited by ActivityLog .getFeed

                  这篇关于使用 JAX-RS 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用休眠 JPA 注释映射嵌套集合 Map&lt;Ke 下一篇:java中的@符号是什么意思?

                  相关文章

                  最新文章

                  <tfoot id='avIef'></tfoot>
                    <legend id='avIef'><style id='avIef'><dir id='avIef'><q id='avIef'></q></dir></style></legend>

                      • <bdo id='avIef'></bdo><ul id='avIef'></ul>

                    1. <i id='avIef'><tr id='avIef'><dt id='avIef'><q id='avIef'><span id='avIef'><b id='avIef'><form id='avIef'><ins id='avIef'></ins><ul id='avIef'></ul><sub id='avIef'></sub></form><legend id='avIef'></legend><bdo id='avIef'><pre id='avIef'><center id='avIef'></center></pre></bdo></b><th id='avIef'></th></span></q></dt></tr></i><div id='avIef'><tfoot id='avIef'></tfoot><dl id='avIef'><fieldset id='avIef'></fieldset></dl></div>

                    2. <small id='avIef'></small><noframes id='avIef'>