有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

简单GET请求项目中的java JAXRS后构造/注入标记

我一直在解构某人的项目,该项目从第三方API检索响应并将其打印到Vaadin web GUI

我的版本应该是从一个动漫网站检索API响应,自动解析(?)输入一个对象,然后将对象属性打印到我的屏幕上,这样我就可以看到它是否有效

我将一个示例XML文件转换为XSD,然后使用JAXB从中生成一个类(以及一个生成器,但我还不确定它是如何使用的),以便存储来自API的响应

我有一个执行get请求的getservice java类。以前,我所做的只是将请求的结果打印到字符串中,然后再尝试将其放入对象中,这样做是有效的

最后,我有一个主要的JavaApplication4类,它显然是创建请求实例所必需的(我对OO编程非常陌生,但可能有点道理)

应用程序正在运行,但我现在收到一条错误消息:

Exception in thread "main" java.lang.NullPointerException
    at javaapplication4.getservice.fetchMovie(getservice.java:36)
    at javaapplication4.JavaApplication4.main(JavaApplication4.java:17)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

这让我明白了

   return target.queryParam("anime", "4658")
            .request(MediaType.TEXT_HTML)
            .header("Content-type", "text/html")//application_json or text_html or xml
            .get(Ann.class);

我使用了一个断点,发现我的客户机和目标在这里和上面的几行中都保持为null:

@PostConstruct
    protected void init() {
        client = ClientBuilder.newClient();
        //target = client.target("http://www.omdbapi.com");
        target = client.target("http://cdn.animenewsnetwork.com/encyclopedia/api.xml");
}

回顾最初的guy项目,我认为问题在于我使用了@PostConstruct之类的注释,但没有@Inject注释。我试图在主/JavaApplication4文件中添加一个@Inject,但它要么没有任何作用(看起来完全错误),要么告诉我它不适用于我放置它的位置

我非常感谢有人能快速查看一下,看看是不是这些注释导致了问题。。。我还不知道如何在任何情况下使用它们,而且很难找到以特定方式做事情的例子,我只是在几个小时内试图重新定位零碎的东西,显然这不起作用

项目的完整代码,减去Ann。java类(应该以动画、标题、名称等形式存储API响应)和ObjectFactory。它旁边生成的java类(我还不确定它是做什么的,但这是另一个步骤):

安。JAVA (我认为是getter和setter以及xml之类的东西)

对象工厂。爪哇

JavaApplication4。爪哇

package javaapplication4;

import generated.Ann;
import javax.inject.Inject;

public class JavaApplication4 {

//@Inject
//getservice gt;

public static void main(String[] args) {

//@Inject
 getservice gt = new getservice(); 


String output = gt.fetchMovie().getAnime().getName();


System.out.println(output);

}
}

获取服务。爪哇

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication4;

import generated.Ann;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author J
 */

@ApplicationScoped
public class getservice {

    private Client client;
    private WebTarget target;

    @PostConstruct
    protected void init() {
        client = ClientBuilder.newClient();
        //target = client.target("http://www.omdbapi.com");
        target = client.target("http://cdn.animenewsnetwork.com/encyclopedia/api.xml");
    }

// these apparently stay null when i run JavaAppliation4
// do i need an @Inject somewhere or something completely similar or different?

    public Ann fetchMovie() {
        //return target.queryParam("anime", "4658")
        return target.queryParam("anime", "4658")
                .request(MediaType.TEXT_HTML)
                .header("Content-type", "text/html")//application_json or text_html or xml
                .get(Ann.class);
    }
}

谢谢。这只是其中一个“我被卡住了,所以我会继续努力”的部分,看起来它不会让我走得很远。。。idk…:)


共 (0) 个答案