简体   繁体   English

从Rest Client请求通过服务到Web应用程序检索JSON

[英]Retrieve JSON from Rest Client Request trough Service To Web Application

I've made a REST client sending http request to a server, receiving reply with JSON message: 我已经制作了一个REST客户端,将Http请求发送到服务器,并收到JSON消息的回复:

public class TestClass {
    public static String getData() {
        String output = null;
        try {

            URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/application.json");//my url i.e fetch data from .
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Content-Type", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode());
            }
            InputStreamReader in = new InputStreamReader(conn.getInputStream());
            BufferedReader br = new BufferedReader(in);

            while ((output = br.readLine()) != null) {
                System.out.println(output); 
            }

            conn.disconnect();

        } catch (Exception e) {
            System.out.println("Exception in TestClass:- " + e);
        }
        return output;
    }
}

When I run the function above, I can see the returned JSON. 当我运行上面的函数时,我可以看到返回的JSON。 Resonse on the server side (API) looks like this: 服务器端(API)的响应如下所示:

@Path("/employees") public class employee {

    @GET
    @Path("/getEmployees")
    public String  createNewMassage() {
        return TestClass.getdata();
    }
}

If I query this link http://localhost/myProject/rest/employees/getEmployees , I don't get any data, only errors: 如果我查询此链接http://localhost/myProject/rest/employees/getEmployees ,那么我没有任何数据,只有错误:

HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception HTTP状态500-Servlet Jersey REST Service的Servlet.init()抛出异常


I tried to change it like: 我试图将其更改为:

@Path("/employees")
public class employee {

    @GET
    @Path("/getEmployees")
    public String  createNewMassage() {
        return "WHY THE TEXT SHOW BUT NOT THE REST CLIENT HTTP REQUEST IN JSON  ";
    }
}

What is the issue? 有什么问题 How can I resolve the problem? 我该如何解决该问题?

First verify if you are getting the response in your client: 首先验证您是否在客户端中得到响应:

System.out.println(TestClass.getdata());

If show the problem is the response. 如果显示问题是响应。

If are the response you can try to add the response as a JSON: 如果是响应,则可以尝试将响应添加为JSON:

@Path("/employees")
public class JSONService {

    @GET
    @Path("/getEmployees")
    @Produces(MediaType.APPLICATION_JSON) //DEFINE RESPONSE
    public String  createNewMassage() {
       return TestClass.getdata();
    }
    //...

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

相关问题 如何使用json从休息Web服务中检索图像 - how to retrieve image from rest web service with json 使用Java客户端应用程序中的JSON Web服务 - Using a JSON web service from a Java client application 如何使用 Quarkus 以两种方式 SSL 从 REST 服务中的请求中检索客户端证书 - How to retrieve the client certificate from a request in a REST service using Quarkus in two way SSL 将带有JSON数据的POST请求从HTML发送到Java Rest Web服务时出现HTTP错误415 - HTTP Error 415 when sending POST request with JSON data from HTML to Java Rest web service 如何从Java中的剩余请求中检索客户端证书 - How to retrieve client certificate from rest request in Java 从客户端应用程序(ReactJs + NodeJs)向Java Web服务发出请求 - Making request from client application (ReactJs+NodeJs) to Java web service Web服务客户端应用程序 - Web service Client application REST Web服务-映射到Java Complex对象的JSON请求 - REST Web service - JSON request mapping to a java Complex object 使用Java将JSON发布请求发送到Rest Web Service - Sending JSON Post Request to Rest Web Service in Java 带有“ true”作为布尔真实值的其余Web服务JSON请求正文 - Rest web service JSON request body with “truee” as boolean true value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM