简体   繁体   中英

Java web service Failed to Load on calling from ReactJS client

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:

在此处输入图片说明

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>

It's the function(data){} that needs the binding, no then() .

Rewrite your code like this and you should be fine:

componentDidMount: function(){
  $.ajax({
    url: "localhost:8080/UserManagement/rest/myService/data"
  }).then((function(data) {
    this.setState({text: data});
  }).bind(this))
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM