2016-04-27 55 views
0

我有一個JAVA RESTful webservice,它將返回JSON字符串,並且它是用Java編寫的。我的問題是,當我發送請求到web服務與以下網址JAVA JSON Restfull WebService在請求的資源上沒有'Access-Control-Allow-Origin'標頭

http://localhost:8080/WebServiceXYZ/Users/insert 

它給了我下面的錯誤消息

XMLHttpRequest cannot load http://localhost:8080/WebServiceXYZ/Users/insert/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 500. 

這裏是我的代碼

package com.lb.jersey; 

import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import org.json.JSONException; 
import org.json.JSONObject; 

@Path("/Users") 
public class RegistrationService 
{ 
    @POST 
    @Produces(MediaType.APPLICATION_JSON) 
    @Path("/insert") 
    public String InsertCredentials (String json) throws JSONException 
    { 
     java.util.Date dt = new java.util.Date(); 
     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     String currentTime = sdf.format(dt); 

     String phone = null; 
     JSONObject returnJson = new JSONObject(); 
     try 
     { 
      JSONObject obj = new JSONObject(json); 
      JSONObject result1 = obj.getJSONObject("Credentials"); 
      phone = result1.getString("phone"); 
      DBConnection conn = new DBConnection(); 

      int checkUserID = conn.GetUserIDByPhone(phone); 
      if(checkUserID <= 0) 
      { 
       DBConnection.InsertorUpdateUsers(phone, currentTime); 
      } 

      int userID = conn.GetUserIDByPhone(phone); 
      int otp = (int) Math.round(Math.random()*1000); 

      DBConnection.InsertorUpdateCredentials(userID, otp, currentTime); 

      JSONObject createObj = new JSONObject(); 
      createObj.put("phone", phone); 
      createObj.put("otp", otp); 
      createObj.put("reqDateTime", currentTime); 
      returnJson.put("Credentials", createObj); 

      System.out.println(returnJson); 
     } 
     catch (Exception e) 
     { 
     } 
     return returnJson.toString(); 
    } 
} 

我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>WebServiceXYZ</display-name> 
    <servlet> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>com.lb.jersey</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

我已經閱讀了很多文章,但沒有取得進展,請讓我知道,我該如何處理這個問題?

回答

2

這是假設您使用碼頭作爲您的服務器。希望它可以幫助...

將以下代碼添加到你的web.xml

<filter> 
<filter-name>cross-origin</filter-name> 
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class> 
<init-param> 
    <param-name>allowedOrigins</param-name> 
    <param-value>*</param-value> 
</init-param> 
<init-param> 
    <param-name>allowedMethods</param-name> 
    <param-value>GET,POST,DELETE,PUT,HEAD</param-value> 
</init-param> 
<init-param> 
    <param-name>allowedHeaders</param-name> 
    <param-value>origin, content-type, accept</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>cross-origin</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 

及以下依賴在你的pom.xml

<dependency> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-servlets</artifactId> 
     <version>8.0.0.M0</version> 
</dependency> 

配置Tomcat是link的鏈接。 ..這是另一個link2相應修改:)

+0

在我的項目中沒有「pom.xml」文件。 – user3441151

+0

你使用的是一個maven項目嗎?如果沒有,那麼你不會有一個pom.xml文件。在這種情況下,下載jar文件並將其手動添加到您的路徑會更好。這是假設您使用碼頭作爲您的服務器 – Abhishek

+0

我沒有使用Maven項目。 – user3441151

0

您可以嘗試設置HttpServletResponse的標頭

import javax.servlet.http.HttpServletResponse; 

@Path("/Users") 
public class RegistrationService 
{ 

    @Context 
    private HttpServletResponse servletResponse; 

    private void allowCrossDomainAccess() { 
     if (servletResponse != null){ 
      servletResponse.setHeader("Access-Control-Allow-Origin", "*"); 
     } 
    } 

    @POST 
    @Produces(MediaType.APPLICATION_JSON) 
    @Path("/insert") 
    public String InsertCredentials (String json) throws JSONException 
    { 
     allowCrossDomainAccess(); 
     // your code here 
    } 
} 
相關問題