简单的基于REST的程序中的HTTP 500内部服务器错误。在接收/发送来自服务器的响应时,在GET和POST中感到困惑 [英] HTTP 500 Internal Server Error in simple REST based program. Confused in GET and POST while receiving/sending response from server

查看:91
本文介绍了简单的基于REST的程序中的HTTP 500内部服务器错误。在接收/发送来自服务器的响应时,在GET和POST中感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在首次使用REST服务实现基本的客户端服务器体系结构。这次我把它作为客户和服务器之间的参数共享类对象来增加更多类和服务的复杂性。我在ApacheTomcat7上运行服务器。它正在成功执行。当我运行我的客户端它给我错误: javax.ws.rs.InternalServerErrorException:HTTP 500内部服务器错误我试着调试我的代码,好像我不是正确接收/发送回复。我知道在这里分享所有课程是不明智的想法,但我没有选择,因为它浪费了我很多时间。任何帮助将不胜感激。提前致谢。

以下是我的ImageProgress类。

  @XmlRootElement 
public class ImageProgress {
private String名称;

public ImageProgress(String image_name){
this.name = image_name;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;


$ / code>

HPCResponse是将其对象返回给客户端的类作为服务器响应。 HPCResponse将基本上返回ImageProgress对象,它将给我预期的结果。

  @XmlRootElement 
公共类HPCResponse
{
私人ImageProgress imgProgress;

public ImageProgress getImgProgress(){
return imgProgress;
}

public void setImgProgress(ImageProgress imgProgress){
this.imgProgress = imgProgress;




$ b $ p
$ b

以下是来自服务器HpcService的服务类,它将返回HPCResponse的对象作为响应。如您所见,startAnalysing方法接受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分析... );

hpcInfo = _hpcInfo;
hpcInfo.getImagePath();

hpcResponse = new HPCResponse();
ImageProgress iProg = new ImageProgress(hpcInfo.getImagePath());
hpcResponse.setImgProgress(iProg);

System.out.println(正在返回响应...);
返回hpcResponse;


$ / code>

HPCInfo类同时位于客户端和服务器端。 HPCInfo类:

  @XmlRootElement 
公共类HPCInfo
{
private String imagePath = ;

public String getImagePath(){
return imagePath;
}

public void setImagePath(String imagePath){
this.imagePath = imagePath;


$ / code>

最后,我的客户端调用HPCService。 / p>

  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();
客户端客户端= ClientBuilder.newClient(clientConfig);
URI serviceURI = UriBuilder.fromUri(webServiceURI).build();

WebTarget webTarget = client.target(serviceURI);

HPCInfo info = new HPCInfo();
info.setImagePath(input);
$ b $ webTarget = webTarget.path(test)。path(hpc)。path(analyze);
$ b $ HPCResponse hResponse = webTarget.request()。accept(MediaType.APPLICATION_XML).post(Entity.entity(info,MediaType.APPLICATION_XML),HPCResponse.class);




$ b这些是我得到的完整的错误描述:

$ b $ / p>

  javax.ws.rs.InternalServerErrorException:HTTP 500内部服务器错误
位于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)
在org.glassfish.jersey.client.JerseyInvocation $ 2.call(JerseyInvocation.java:683)
在org.glassfish.jersey.internal.Errors.process(Errors.java :315)
在org.glassfish.jersey.internal.Errors.process(Errors.java:297)
在org.glassfish.jersey.internal.Errors.process(Errors.java:228)
在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
在org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:435)
在org.glassfish.jersey.client.JerseyInvocation $ Builder.post(JerseyInvocation.java:338)
at com.TestClient.main(TestClient.java:34)


解决方案

<这很可能是由于 ImageProgress 类没有默认的(无参数)构造函数。这是JAXB所必需的。



调试这种类型的一种方法是创建一个简单的 ExceptionMapper 来捕获异常未映射的。当没有映射器的时候,通常这个异常会冒泡到容器级别,这只会给我们带来通用的500服务器错误(大多数情况下帮助不大)。

  @Provider 
public class DebugExceptionMapper实现ExceptionMapper< Exception> {

@Override
public ResponseResponse(异常异常){
exception.printStackTrace();
return Response.serverError()。entity(exception.getMessage())。build();
}
}

然后只需注册映射器。使用 ImageProgress 类运行简单测试时,抛出异常时,将打印堆栈跟踪,您可以看到异常消息


... ImageProgress没有无参数的默认构造函数



I am implementing a basic client server architecture using REST services for the first time. This time I making it more complicated with including some more classes and services with sharing class objects as parameters between client and server. I am running server on ApacheTomcat7. It is getting executed successfully. When I am running my client it is giving me error: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error I tried debugging my code, it seems like I am not properly receiving/sending response. I know its not wise idea to share all classes here but I has no choice since it has wasted my time a lot. Any help will be appreciated. Thanks in advance.

Following is my ImageProgress class. This class is present at both server and client.

@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 is the class whose object will be returned to client as the server response. HPCResponse will basically return the ImageProgress object which will give me the intended result.

@XmlRootElement
public class HPCResponse
{
    private ImageProgress imgProgress;

    public ImageProgress getImgProgress() {
        return imgProgress;
    }

    public void setImgProgress(ImageProgress imgProgress) {
        this.imgProgress = imgProgress;
    }
}

Following is the service class from server named HpcService which will return the HPCResponse's object as response. As you can see the method startAnalysing accepts object of HPCInfo. Description of HPCInfo is also given below.

@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 class is also at both client and server. HPCInfo class:

    @XmlRootElement
    public class HPCInfo
    {
        private String imagePath = "";

        public String getImagePath(){
            return imagePath;
        }

        public void setImagePath( String imagePath ){
            this.imagePath = imagePath;
        }
    }

And finally its my client calling for the 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);
    }
}

This is the full error description I am getting:

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)

解决方案

This is most likely due to ImageProgress class not having a default (no-arg) constructor. This is required from JAXB.

One way to debug things like this is to create a simple ExceptionMapper to catch exceptions that are not mapped. When there is no mapper, often the exception will bubble up to the container level, which just gives us generic 500 server error (which most of the time is of little help).

@Provider
public class DebugExceptionMapper implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(Exception exception) {
        exception.printStackTrace();
        return Response.serverError().entity(exception.getMessage()).build();
    } 
}

Then just register the mapper. When running a simple test with your ImageProgress class, when the exception is thrown, the stacktrace gets printed, and you can see the exception message

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

这篇关于简单的基于REST的程序中的HTTP 500内部服务器错误。在接收/发送来自服务器的响应时,在GET和POST中感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆