RESTful服务器似乎无法正常运行

时间:2019-08-25 09:22:14

标签: java rest web-services

我正在使用NetBeans,并且想要运行本地服务器,如本教程中所述:https://www.baeldung.com/apache-cxf-rest-api 问题如下:我运行服务器端点,然后运行测试,并得到以下错误。

java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/course/courses/1
at tests.RestTest.getCourse(RestTest.java:50)
at tests.RestTest.firstTest(RestTest.java:67)

现在,服务器仅管理课程列表和尝试课程的学生。服务器端点的代码如下:

public class RestfulServer {
    public static void main(String[] args){
        String address = "http://localhost:8080/";
        System.out.println("Initializing server...");
        JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
        factoryBean.setResourceClasses(CourseRepository.class);
        factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
        factoryBean.setAddress(address);
        System.out.println("Server address is " + address);
        Server server = factoryBean.create();
        System.out.println("Server running...");
    }
}

这是测试类:

public class RestTest {

    private static String BASE_URL = "http://localhost:8080/course/courses/";
    private static CloseableHttpClient client;

    public RestTest() {
    }

    @BeforeClass
    public static void setUpClass() {
        client = HttpClients.createDefault();
    }

    @AfterClass
    public static void tearDownClass() throws IOException {
        client.close();
    }

    private Course getCourse(int courseOrder) throws IOException {
        URL url = new URL(BASE_URL + courseOrder);
        InputStream input = url.openStream();
        Course course
          = JAXB.unmarshal(new InputStreamReader(input), Course.class);
        return course;
    }

    @Test
    public void firstTest() throws IOException{
        Assert.assertEquals("REST with Spring", getCourse(1).getName());
    }
}

我想您想知道此方法的去向,所以这是CourseRepository类,它是本地数据库(只是一堆字符串)和@ GET,@ PUT等方法的位置。

@Path("course")
@Produces("text/html")
public class CourseRepository {
    private Map<Integer, Course> courses = new HashMap<>();

    //other things not useful now...

private Course findById(int id) {
    for (Map.Entry<Integer, Course> course : courses.entrySet()) {
        if (course.getKey() == id) {
            return course.getValue();
        }
    }
    return null;
}

@GET
@Path("courses/{courseId}")
public Course getCourse(@PathParam("courseId") int courseId) {
    return findById(courseId);
}

现在,我不明白的是为什么我在运行测试时遇到先前的错误。就像我输入了错误的URL,但我不确定。

编辑: 为了回答JB Nizet,我尝试调试服务器,然后封装

return findById(courseId);

在try / catch语句中,因为似乎是导致执行的方法。尽管“捕获”根本不捕获任何内容,即使执行在方法执行后立即出现。此外,我刚刚注意到服务器输出窗口中出现了一个新警告:

AVVERTENZA: Both com.bankrestserver.CourseRepository#getCourse and com.bankrestserver.CourseRepository#getCourse are equal candidates for handling the current request which can lead to unpredictable results
ago 25, 2019 12:15:36 PM org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem

GRAVE: No message body writer has been found for class com.bankrestserver.Course, ContentType: text/html

0 个答案:

没有答案