java官方 jax rs_Java-JAX-RS依赖项注入

我已经使用Spring Rest完成了项目.现在,我们有一个小型休息项目,并计划与Jersey JAX-RS一起使用.我是新手,因此推荐SO和其他博客来成功实现具有依赖项注入的Rest api.

有以下代码.

AppConfig.java

import javax.ws.rs.ApplicationPath;

import javax.ws.rs.core.Application;

@ApplicationPath("/")

public class AppConfig extends Application {

@Override

public Set> getClasses() {

System.out.println("AppConfig");

final Set> s = new HashSet>();

s.add(Controller.class);

s.add(AppFeature.class);

return s;

}

}

AppBinder.java

import org.glassfish.hk2.utilities.binding.AbstractBinder;

public class AppBinder extends AbstractBinder {

@Override

protected void configure() {

System.out.println("AppBinder");

bind(ReflectionService.class).to(ReflectionService.class);

}

}

AppFeature.java

import javax.ws.rs.core.Feature;

import javax.ws.rs.core.FeatureContext;

public class AppFeature implements Feature {

@Override

public boolean configure(FeatureContext context) {

System.out.println("AppFeature");

context.register(new AppBinder());

return true;

}

}

Controller.java

@Path("/")

public class Controller {

@Inject

Service service;

public Controller(){

System.out.println("Controller created");

}

// other methods

}

Service.java

@Singleton

public class Service

public Service(){

System.out.println("Service instance created");

}

// other methods

}

我假设Controller和Service的每个实例都是在Tomcat 8服务器启动时创建的,并且依赖项注入已完成.但是在启动过程中,将其放在控制台上

INFO: Registering the Jersey servlet application, named

com.sample.auto2.AppConfig, at the servlet mapping /*, with the

Application class of the same name.

AppConfig

AppConfig

Nov 15, 2016 12:22:20 PM org.glassfish.jersey.server.ApplicationHandler initialize

INFO: Initiating Jersey application, version Jersey: 2.6 2014-02-18

21:52:53…

AppFeature

AppBinder

Nov 15, 2016 12:22:21 PM

org.apache.catalina.startup.HostConfig deployDirectory

每次,我们发送一个请求,在控制台中得到关注

Service instance created

Controller created

我的问题

>服务,每当我们发送一个

http请求;它在每个请求中创建实例还是只是

调用构造函数?

>为什么AppConfig中的System.out被调用两次?

>是否有更好的方法来设置我的小型项目,该项目没有任何数据库访问权限并且只有三个发布端点?

编辑:

根据@Harikrishnan提供的链接,为Controller类添加了@Singleton.现在,构造函数仅调用一次(在第一个请求时-为什么在服务器启动期间不!).

但是,为什么Service类构造函数会在每个请求上调用(在将@Singleton添加到Controller之前),即使它是单例的呢?另外,其他问题仍然存在.

编辑2:

谢谢@peeskillet.所以这些对我来说是结果.

>这在第一次请求时仅调用一次构造函数

bind(ReflectionService.class).to(ReflectionService.class).in(Singleton.class);

bind(Controller.class).to(Controller.class).in(Singleton.class);

>这给http请求错误

bind(ReflectionService.class).to(ReflectionService.class).in(Immediate.class);

java.lang.IllegalStateException: Could not find an active context for org.glassfish.hk2.api.Immediate

没关系,因为

>这在服务器启动时称为构造函数,仅一次

bind(new ReflectionService()).to(ReflectionService.class);

bind(new Controller()).to(Controller.class);

>这样,启动时就完成了注入,但是http请求上却出现404错误. (在AppBinder中配置了控制器,然后在AppConfig中配置了)

@ApplicationPath("/")

public class AppConfig extends ResourceConfig {

public AppConfig() {

register(new AppBinder());

}

}

>就像您所说的那样,它可以运行!

@ApplicationPath("/")

public class AppConfig extends ResourceConfig {

public AppConfig() {

register(new AppBinder());

register(Controller.class);

}

}

最后,这些都是我所需要的

public class AppBinder extends AbstractBinder {

@Override

protected void configure() {

bind(new ReflectionService()).to(ReflectionService.class);

bind(new Controller()).to(Controller.class);

}

}

@ApplicationPath("/")

public class AppConfig extends ResourceConfig {

public AppConfig() {

register(new AppBinder());

register(Controller.class);

}

}

解决方法:

Service,Controller constructors is being called whenever we send an http request; does it create instances in each request or just calling constructor?

不幸的是,@ Singleton在使用AbstractBinder绑定时没有任何作用.我们需要明确地说应该是单身

bind(ReflectionService.class).to(ReflectionService.class).in(Singleton.class);

默认行为是“每次查询”范围,这意味着每次请求服务时都会创建一个新实例

(at the very first request – Why not during server startup!!)

这就是它的工作方式.还有一个InstantScope,它将使它在启动时创建

bind(ReflectionService.class).to(ReflectionService.class).in(ImmediateScope.class)

或者,您可以使用实例代替类,它将自动成为单例

bind(new ReflectionService()).to(ReflectionService.class)

Service,Controller constructors is being called whenever we send an http request; does it create instances in each request or just calling constructor?

这是默认行为.每个请求的资源类的新实例.如前所述,如果只需要一次实例,则将其标记为@Singleton

Why System.out in AppConfig is called twice?

不确定,可能只是引导程序上的Jersey内部处理所需的

Is there a better way for setting up my small project, which does not have any db access and only three post endpoints?

设置很好,但是如果使用Jersey,则应使用ResourceConfig(应用程序扩展)类而不是Application

@ApplicationPath("/")

public class AppConfig extends ResourceConfig {

public AppConfig() {

register(new AppBinder());

register(Controller.class);

}

}

这样,您无需将AppBinder包装在AppFeature中.泽西岛已经知道如何处理AppBinder来源:https://www.icode9.com/content-1-558351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以按照以下步骤将JAX-WS接口替换为JAX-RS接口: 1. 创建JAX-RS接口:创建一个新的Java接口来定义您的JAX-RS服务。在接口上使用`@Path`注解指定资源的URL路径。 ```java import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/your-resource") public interface YourResource { @GET @Produces(MediaType.APPLICATION_JSON) String getResource(); } ``` 2. 实现JAX-RS接口:创建一个类来实现您的JAX-RS接口,并实现接口中定义的方法。 ```java public class YourResourceImpl implements YourResource { @Override public String getResource() { // 实现您的业务逻辑 return "Hello JAX-RS!"; } } ``` 3. 注册JAX-RS服务:将您的JAX-RS服务注册到应用程序中。这可以通过创建一个`javax.ws.rs.core.Application`子类并在其中注册资源类来完成。 ```java import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; @ApplicationPath("/api") public class YourApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<>(); classes.add(YourResourceImpl.class); return classes; } } ``` 4. 配置JAX-RS:根据您使用的应用程序服务器,将JAX-RS的实现(如Jersey或RestEasy)添加到您的应用程序的构建配置文件中。您还需要确保在应用程序服务器上正确配置JAX-RS。 5. 测试JAX-RS接口:启动您的应用程序服务器,并使用JAX-RS客户端或浏览器等工具测试您的JAX-RS接口。 请注意,以上步骤是一般的指导,具体步骤可能因您使用的框架和工具而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值