首頁>Program>source

我第一次使用REST服務實現基本的客戶端服務器體系結構.這次,通過在客戶端和服務器之間共享類物件作為引數来包含更多類和服務,使我變得更加複雜.我在ApacheTomcat7上執行服務器.它正在成功執行.当我執行客戶端時,它给我錯誤: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error 我尝試除錯我的代碼,看来我没有正確接收/發送响應.我知道在這裏共享所有Class程不是一个明智的主意,但是我別無選擇,因為這浪费了我很多時間.任何帮助將不胜感激.提前致谢.

以下是我的ImageProgress類.此類在服務器和客戶端上都存在。

@XmlRootElement
public class ImageProgress{
    private String name;
    public ImageProgress( String image_name){
        this.name = image_name;
    }
    public String getName() {
        return name;
    }
    public void setName( String name ){
        this.name = name;
    }
}

HPCResponse是其物件將作為服務器响應返迴给客戶端的類. HPCResponse基本上將返迴ImageProgress物件,该物件將為我提供預期的結果。

@XmlRootElement
public class HPCResponse
{
    private ImageProgress imgProgress;
    public ImageProgress getImgProgress() {
        return imgProgress;
    }
    public void setImgProgress(ImageProgress imgProgress) {
        this.imgProgress = imgProgress;
    }
}

以下是名為HpcService的服務器中的服務類,它將返迴HPCResponse的物件作為响應.如您所见,方法startAnalysing接受HPCInfo的物件. HPCInfo的描述也在下面。

@Path( "/hpc" )
@Consumes( MediaType.APPLICATION_XML )
@Produces( MediaType.APPLICATION_XML )
public class HpcService{
    public HPCInfo hpcInfo;
    public HPCResponse hpcResponse;
    @POST
    @Path( "/analyze" )
    public HPCResponse startAnalysing(HPCInfo _hpcInfo){
        System.out.println( "Started Analyzing..." );
        hpcInfo = _hpcInfo;
        hpcInfo.getImagePath();        
        hpcResponse = new HPCResponse();
        ImageProgress iProg = new ImageProgress(hpcInfo.getImagePath());
        hpcResponse.setImgProgress(iProg);
        System.out.println("Returning response...");
        return hpcResponse;
    }
}

HPCInfo類也在客戶端和服務器上. HPCInfo類:

   @XmlRootElement
    public class HPCInfo
    {
        private String imagePath = "";
        public String getImagePath(){
            return imagePath;
        }
        public void setImagePath( String imagePath ){
            this.imagePath = imagePath;
        }
    }

最後,它是我的客戶要求的HPCService。

public class TestClient {
    private static String webServiceURI = "http://localhost:8080/TestServer123";
    public static void main(String[] args) {
        String input = "ABNKidney.scn";
        ClientConfig clientConfig = new ClientConfig();
        Client client = ClientBuilder.newClient(clientConfig);
        URI serviceURI = UriBuilder.fromUri(webServiceURI).build();
        WebTarget webTarget = client.target(serviceURI);
        HPCInfo info = new HPCInfo();
        info.setImagePath(input);
        webTarget = webTarget.path("test").path("hpc").path("analyze");
        HPCResponse hResponse = webTarget.request().accept(MediaType.APPLICATION_XML).post(Entity.entity(info, MediaType.APPLICATION_XML), HPCResponse.class);
    }
}

這是我得到的完整錯誤描述:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:968)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:795)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:435)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:338)
    at com.TestClient.main(TestClient.java:34)
最新回復
  • 5月前
    1 #

    一種除錯此類事情的方法是建立一个簡單的 ExceptionMapper 捕获未對映的異常.如果没有對映器,則異常通常会上升到容器級別,這只会给我们帶来500服務器通用錯誤(大多數情况下几乎没有帮助)。

    @Provider
    public class DebugExceptionMapper implements ExceptionMapper<Exception> {
        @Override
        public Response toResponse(Exception exception) {
            exception.printStackTrace();
            return Response.serverError().entity(exception.getMessage()).build();
        } 
    }
    

    然後只需註册對映器.用 ImageProgress執行簡單測試時 類,当引發異常時,將顯示堆棧跟蹤,並且您可以看到異常訊息

    ...ImageProgress does not have a no-arg default constructor

    因此只需向 ImageProgress添加一个預設(無引數構造函式) 類.這是JAXB模型的要求。

  • 5月前
    2 #

    class HPCResponse之前添加此代碼行 :

    @XmlAccessorType(XmlAccessType.FIELD)
    

  • iphone:如何从我的應用程式打開"設置"應用程式?
  • 用Java讀寫TIFF圖像