【问题标题】:Jersey : Error creating a JAXBContext for wadl processing泽西岛:为 wadl 处理创建 JAXBContext 时出错
【发布时间】:2018-05-01 12:41:54
【问题描述】:

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

从 paw 之类的 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

【问题讨论】:

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


    【解决方案1】:

    这指出了 CORS 的问题。

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

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

    查看这篇文章https://www.html5rocks.com/en/tutorials/cors/

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2012-06-04
      相关资源
      最近更新 更多