我的@Produces方法有问题。当我在Api Request类中注入ResteasyWebTarget目标时,对象target为null。
有人可以帮我吗? CDI在我的课程中不起作用...
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, METHOD, PARAMETER, FIELD})
public @interface ServiceProducer {
}
公共类ServiceProducerImpl实现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();
}
}
我可以使用这个:
@Stateless
public class ApiRequest {
//Content of class.....
}
Class Main:公共类主要{
@EJB
ApiRequest apiRequest;
public static void main(String[] args) {
Main main = new Main();
main.executeRest();
}
public void executeRest(){
apiRequest.rest();
}
等于返回NullPointerException,