Ajax调用没有达到返回404错误的其余服务

时间:2017-03-07 05:48:39

标签: java jquery ajax rest jersey

我是其余api的新用户,并尝试使用jquery ajax命中服务方法。主要问题是,服务类没有被调用,我从服务器得到错误404。 这是我的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>AudienceManagement</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</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.inf.dashboardapp.api</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>

这是我的js文件:

var myData=[];
 $('#ajax').click(function(){ 
        alert('ajax');
         $.ajax({ 
             type: "GET",
             dataType: "json",
             url: "http://localhost:8080/AudienceManagement/api/ref/check",
             success: function(data){        
                alert(data);
                myData=data;
             },
             error: function(data){
                 alert('error');
               },
               complete: function(data) {
                 alert('complete')
               }
         });
    });

这是我的服务类。

@Path("ref")
public class DataAPI {
    @Path("check")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response fetchBoatingTypeList() {
        System.out.println("Entered");
        String returnValue = null;
        Response response = null;
        try {
            JSONObject obj = new JSONObject();
             obj.put("name", "Bhavana");
              obj.put("num", new Integer(100));
              obj.put("balance", new Double(1000.21));
              obj.put("is_vip", new Boolean(true));
            response = Response.status(Status.OK).entity(obj).build();
        } catch (Exception e) {
            //String returnString = "{\"message\":\""
                    //+ AppConfig.PROPERTIES.getProperty(e.getMessage()) + "\"}";
            String returnString="ERRRRRRROR ";
            response = Response.status(Status.SERVICE_UNAVAILABLE)
                    .entity(returnString).build();
        }
        return response;
    }
}

这是我在进行ajax调用时遇到的错误。

GET http://localhost:8080/AudienceManagement/api/ref/check 404 (Not Found)

0 个答案:

没有答案