Jersey:为wadl处理创建JAXBContext时出错

时间:2018-05-01 12:41:54

标签: java angular web-services jersey jax-rs

我有一个用泽西编码的REST API需要身份验证。要进行身份验证,您需要使用正文中的凭据发布到此网址:http://localhost:8080/login

从像爪子这样的API工具发布时工作正常但是我无法使用Angular 5,我总是在浏览器控制台中收到此错误:

Response to preflight request doesn't pass access control check: No 
Access-Control-Allow-Origin' header is present on the requested resource

这就是服务器控制台:

WARNING [http-nio-8080-exec-11] 
org.glassfish.jersey.internal.Errors.logErrors The following warnings 
have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 2
javax.ws.rs.ProcessingException: Error creating a JAXBContext for 
wadl processing.

Complete Stacktrace

这是我的配置

资源:

@POST
@Path("login")
public Response getSession (Login login, @Context HttpServletRequest req) {

    System.out.println(login.getUsername());
    System.out.println(login.getPassword());

    LDAP ldap = new LDAP();

    String role = "none";

    try {
        if (ldap.auth(login.getUsername(), login.getPassword())) {
            role = ldap.getRole(login.getUsername());
        }

    } catch (Exception e) {

        log.error(e);

        throw new NotAuthorizedException(e.getMessage(), Response.Status.UNAUTHORIZED);

    }


    HttpSession session = req.getSession(true);
    session.setAttribute("user", login.getUsername());
    session.setAttribute("role", role);

    return Response.status(Response.Status.OK)
            .entity(session.getId())
            .build();

}

Pom.xml:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.26</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-bean-validation</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>
<properties>
    <jersey.version>2.26</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

角度代码:

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'application/json'
    })
};

login(username, password) {
    const body = JSON.stringify({'username' : username, 'password' : password});
    return this.http.post(baseURI, body, httpOptions);
}

我正在使用Java SE 9.0.1和angular 1.7.3

1 个答案:

答案 0 :(得分:0)

这指出了CORS的问题。

Access-Control-Allow-Origin' header is present on the requested resource

出于安全原因,服务器仅接受具有相同来源的请求。您可以尝试为服务器响应设置Access-Control-Allow-Origin: *

查看本文https://www.html5rocks.com/en/tutorials/cors/

希望这会对你有所帮助