如何添加" Access-Control-Allow-Origin" Java RESTful Web服务的标头?

时间:2015-01-15 09:23:30

标签: java web-services rest

我创建了一个在IIS中运行良好的java RESTful Web服务。但是当我在服务器上部署它之后尝试在Dev环境中测试它时,它会抛出以下错误:

XMLHttpRequest无法加载https://localhost:8443/RESTfulWS/rest/UserInfoService/NLP/Mumbai%20is%20the%20financial%20capital%20of%20India?xml。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' https://hpscrmdev.honeywell.com'因此不允许访问。

据我所知,我需要为某个特定域可以访问的资源(即Web服务)设置一些标头,但我不知道如何实现它。我不知道我是否在这里有意义,因为我是开发Web服务的新手。以下是Web服务的以下代码。

package com.eviac.blog.restws;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

// @Path here defines class level path. Identifies the URI path that 
// a resource class will serve requests for.
@Path("UserInfoService")
public class UserInfo {
private static final String plain = null;
// @GET here defines, this method will method will process HTTP GET requests.
@GET
// @Path here defines method level path. Identifies the URI path that a
// resource class method will serve requests for.
@Path("/name/{i}")
// @Produces here defines the media type(s) that the methods
// of a resource class can produce.
@Produces(MediaType.TEXT_XML)
// @PathParam injects the value of URI parameter that defined in @Path
// expression, into the method.
    public String userName(@PathParam("i") String i) {
    String name = i;
    return "<User>" + "<Name>" + name + "</Name>" + "</User>";
    }

@GET 
@Path("/age/{j}") 
@Produces(MediaType.TEXT_XML)
    public String userAge(@PathParam("j") int j) {

    int age = j;
    return "<User>" + "<Age>" + age + "</Age>" + "</User>";
    }

@GET 
@Path("/NLP/{k}")

@Produces(MediaType.TEXT_XML)
    public String nlpPOS(@PathParam("k") String k) {

    String description = k;
    POSTagger obj = new POSTagger();
    ArrayList<String> list = obj.tagging(description);

    String[] result = list.toArray(new String[list.size()]);

    String resultXML = "<result>";
        for (String s : result) {
            resultXML = resultXML + "<noun>" + s + "</noun>"; 
        }
        resultXML = resultXML + "</result>";

        return resultXML;
    }
}

我只使用nlpPOS方法。我还有另一个类,它包含用于执行Web服务的tagging()方法。有人可以帮我在这个代码中设置标题的位置吗?

非常感谢任何帮助。

谢谢! Chiranjit

0 个答案:

没有答案