Java web service Failed to Load on calling from ReactJS client

时间:2017-10-12 09:56:43

标签: java reactjs web-services

I have created a web service in Java and I have deployed it over Tomcat Apache (version 8.5). Moreover, I have written a simple client in ReactJS that connects to my web service and displays the response. But I am getting error when I run my client. Please tell what can be the issue?

Error:

enter image description here

MyServer.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.json.JSONObject;


@Path("myService")
public class MyServer {

    @GET
    @Path("/data")
    @Consumes(MediaType.APPLICATION_JSON)

    public Response queryRESTService(InputStream incomingData) {

        JSONObject jsonObject = null;
        try {
            String output = "";
            InputStream inputStream = new FileInputStream("C:\\Users\\username\\myJSON.txt");
            InputStreamReader inputReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                output += line + "\n";
            }
            jsonObject = new JSONObject(output);
            bufferedReader.close();
        }
        catch (Exception e) {
        }
        return Response.status(200).entity(jsonObject.toString()).build();
    }

    @GET
    @Path("/verify")
    @Produces(MediaType.TEXT_PLAIN)
    public Response verifyRESTService() {
        String result = "MyRESTService Successfully started..";
        return Response.status(200).entity(result).build();
    }

}

web.xml

<?xml version = "1.0" encoding = "UTF-8"?> 
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
   xmlns = "http://java.sun.com/xml/ns/javaee"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
   id = "WebApp_ID" version = "3.0"> 
   <display-name>User Management</display-name> 
   <servlet> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
      <init-param> 
         <param-name>jersey.config.server.provider.packages</param-name> 
         <param-value>com.tutorialspoint</param-value> 
      </init-param> 
   </servlet> 
   <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/rest/*</url-pattern> 
   </servlet-mapping>   
</web-app>

Client.html

<!DOCTYPE html>
<html>
    <head>
        <title>React Flux</title>
        <script src="https:/fb.me/react-0.13.3.js"></script>
        <script src="https:/fb.me/JSXTransformer-0.13.3.js"></script>
        <script src="http:/code.jquery.com/jquery-2.1.4.min.js"></script>
    </head>
    <body>
        <div id="component"></div>

        <script type="text/jsx">
            var JavaEEWSTest = React.createClass({
                getInitialState: function () {
                    return {text: ''};
                },
                componentDidMount: function(){
                    $.ajax({
                        url: "localhost:8080/UserManagement/rest/myService/data"
                    }).then(function(data) {
                        this.setState({text: data});
                    }).bind(this)
                },
                render: function() {
                    return <div>Response - {this.state.text}</div>;
                }
            });

            React.render(<JavaEEWSTest />, document.getElementById('component'));

        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

需要绑定的 function(data){} ,没有 then()

&#xA;&#xA;

像这样重写你的代码你应该没问题:

&#xA;&#xA;
  componentDidMount:function(){&#xA; $ .ajax({&#xA; url:“localhost:8080 / UserManagement / rest / myService / data”&#xA;})。then((function(data){&#xA; this.setState({text: data});&#xA;})。bind(this))&#xA;},&#xA;  
&#xA;