Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

java - @Produces方法在注入时返回null(Method @Produces return null when is inject)

I have a problem with my method @Produces.

(我的方法@Produces有问题。)

When I inject ResteasyWebTarget target in my Api Request class, object target is null.

(当我在Api Request类中注入ResteasyWebTarget目标时,对象target为null。)

Can someone help me with this.

(有人可以帮我弄这个吗。)

CDI not working in my class...

(CDI在我的课堂上不起作用...)

    @Qualifier
    @Retention(RetentionPolicy.RUNTIME)
    @Target({TYPE, METHOD, PARAMETER, FIELD})
    public @interface ServiceProducer {

    }
public class ServiceProducerImpl implements Serializable {

    @Produces
    @ServiceProducer
    public ResteasyWebTarget getClient() {
        String patApi = "http://localhost:5000";
        try {
            ResteasyClient client = new ResteasyClientBuilder().build();
            ResteasyWebTarget target = client.target(UriBuilder.fromPath(patApi));
            return target;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
  @Path("/api-java")
  public interface IServices {

      @PUT
      @Path("/put")
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      Response putservice(ApiRequestModel api);
  }
public class ApiRequest {

    @Inject
    @ServiceProducer
    ResteasyWebTarget target;

    public void rest() {
        String patApi = "http://localhost:5000";
        try {

            IServices service = target.proxy(IServices.class);
            ApiRequestModel api = new ApiRequestModel(11, "22", 0);
            Response response = service.putservice(api);
            ApiResponseModel apiResponse = response.readEntity(ApiResponseModel.class);
            System.out.println("API-JAVA>> " + "CNPJ: " + apiResponse.getCnpj() + " ADQ: " + apiResponse.getAdq() + " BLOCKCODE: " + apiResponse.getBlockcode());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
  ask by Luis Angel Carbonel Hidalgo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can use this:

(我可以用这个:)

@Stateless
public class ApiRequest {
//Content of class.....
}

Class Main:

(主班:)

public class Main {

    @EJB
    ApiRequest apiRequest;

    public static void main(String[] args) {
        Main main = new Main();
        main.executeRest();

    }

    public void executeRest(){
        apiRequest.rest();
    }

Equal return NullPointerException ,

(等于return NullPointerException ,)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...