简体   繁体   English

基于简单 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

I am implementing a basic client server architecture using REST services for the first time.我第一次使用 REST 服务来实现一个基本的客户端服务器架构。 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.我在 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.当我运行我的客户端时,它给了我错误: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error我尝试调试我的代码,似乎我没有正确接收/发送响应。 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.以下是我的 ImageProgress 类。 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 是其对象将作为服务器响应返回给客户端的类。 HPCResponse will basically return the ImageProgress object which will give me the intended result. HPCResponse 基本上会返回 ImageProgress 对象,它会给我预期的结果。

@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.以下是来自名为 HpcService 的服务器的服务类,它将返回 HPCResponse 的对象作为响应。 As you can see the method startAnalysing accepts object of HPCInfo.如您所见,方法 startAnalysing 接受 HPCInfo 对象。 Description of HPCInfo is also given below. 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 class is also at both client and server. HPCInfo 类也在客户端和服务器上。 HPCInfo class: HPCInfo 类:

    @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.最后是我的客户要求 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)

One way to debug things like this is to create a simple ExceptionMapper to catch exceptions that are not mapped.调试此类事情的一种方法是创建一个简单的ExceptionMapper来捕获未映射的异常。 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).当没有映射器时,异常通常会冒泡到容器级别,这只会给我们带来通用的 500 服务器错误(大多数情况下几乎没有帮助)。

@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类运行简单测试时,抛出异常时,会打印堆栈跟踪,您可以看到异常消息

...ImageProgress does not have a no-arg default constructor ...ImageProgress 没有无参数的默认构造函数

So just add a default (no-arg constructor) to the ImageProgress class.因此,只需向ImageProgress类添加一个默认值(无参数构造函数)即可。 This is a requirement with JAXB models.这是 JAXB 模型的要求。

class HPCResponse之前添加这行代码:

@XmlAccessorType(XmlAccessType.FIELD)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 500:发送 Post 请求时发生内部服务器错误 - 500:Internal Server Error while sending Post Request HTTP 状态 500 – 从 java 邮件发送邮件时发生内部服务器错误 - HTTP Status 500 – Internal Server Error While sending a mail from java mail 从Glassfish Jersey Rest服务器获取响应Json对象时获取HTTP错误:500 - Get HTTP ERROR: 500 on get response Json Object from glassfish Jersey Rest server POST http:// localhost:8080 / server2_0_war_exploded /用户返回的响应状态为500 Internal Server Error - POST http://localhost:8080/server2_0_war_exploded/user returned a response status of 500 Internal Server Error REST Jersey 导致 HTTP 状态 500 - 内部服务器错误 - REST Jersey causes HTTP Status 500 - Internal Server Error 获取 HTTP 状态 500 ? 将 tomcat 7 升级到 8 时出现内部服务器错误 - Getting HTTP Status 500 ? Internal Server Error while upgrading tomcat 7 to 8 HTTP / 1.1 500 Internal Server Error网页正常运行时获取Java - HTTP/1.1 500 Internal Server Error Get while webpage works Java Spring Boot 使用 Post 方法获取 500 内部服务器错误 - Spring Boot Get a 500 Internal Server Error with Post method 500 Internal Server Error球衣架 - 500 Internal Server Error jersey rest httpClient-&gt; HTTP / 1.1 500内部服务器错误 - httpClient -> HTTP/1.1 500 Internal Server Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM