REST Web服务构造函数中的端点注册会出错

时间:2014-11-11 06:06:04

标签: java web-services rest jersey

我编写了一个REST Web服务,然后调用web.py.我需要在其构造函数中注册某些端点。在这样做时,它给了我Servlet.init错误(500)。 在下面找到我的代码。这是正确的方法吗?如果没有,我该如何编写init()函数?

这是我的代码:

@Path("/xyz/api")
public class Service {
Extensions ext = new Extensions();
String parentUrl = "http://10.112.1.2";

Service() {
    ext.logInformation("managedEntity");        // call to register endpoint
    parentUrl = "http://localhost:8080";
}

@POST
@Path("/doSomething/{input}")
@Consumes(MediaType.TEXT_PLAIN)
public String createNetwork(@PathParam("input") String input) {

    try {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(parentUrl + "/doSomething?input=" + input);

        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
                new DefaultHttpMethodRetryHandler(3, false));

        try {
            int statusCode = client.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }
            byte[] responseBody = method.getResponseBody();
            System.out.println(new String(responseBody));
        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        } 
    } catch(Exception e) {
        e.printStackTrace();
    }
    System.out.println(result);
    return result.toString();
}

}

0 个答案:

没有答案