Jersey Rest服务仅返回404

时间:2019-06-18 12:30:26

标签: javascript java maven tomcat jersey

对于我的应用程序,我使用REST服务检查帐户数据。我的Java类AuthenticationResource检查此数据,然后将密钥发送回客户端。一切正常,直到我尝试将其赶到gitHub上。这搞乱了我的项目,我不得不做一些事情。 现在一切都像它破裂之前一样。但是REST服务会继续发送404。

我试图更改web.xml和pom.xml中的所有内容,但这没有用。我还检查了Java类本身。我看不到任何问题

这是AuthenticationResource.Java:

package nl.hu.v1ipass.barapp.webservices;

@Path("/authentication")
public class AuthenticationResource {
  final static public SecretKey key = MacProvider.generateKey();

  @POST
  @Produces("application/json")
  public Response authenticateUser( @FormParam("kassanummer") int kassa,
                                    @FormParam("username") int username, 
                                    @FormParam("password") String password) throws SQLException {
    try {
      // Authenticate the user against the database
      UserPostgressDaoImpl dao = new UserPostgressDaoImpl();
      String role = dao.findRoleForUser(kassa, username, password);

      if (role == null) { throw new IllegalArgumentException("No user found!");  } 

      String token = createToken(Integer.toString(username), role);

      SimpleEntry<String, String> JWT = new SimpleEntry<String, String>("JWT", token);
      return Response.ok(JWT).build();


    } catch (JwtException | IllegalArgumentException e) 
        { return Response.status(Response.Status.UNAUTHORIZED).build(); }
  }

  private String createToken(String username, String role) throws JwtException {
        Calendar expiration = Calendar.getInstance();
        expiration.add(Calendar.MINUTE, 30);

        return Jwts.builder()
          .setSubject(username)
          .setExpiration(expiration.getTime())
          .claim("role", role)
          .signWith(SignatureAlgorithm.HS512, key)
          .compact();
      }
} 

我的Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee web-app_4_0.xsd"
    version="4.0">

    <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>nl.hu.v1ipass.barapp.webservices</param-value>
        </init-param> 

        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature
            </param-value>
        </init-param>

        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/restservices/*</url-pattern>
    </servlet-mapping>
</web-app>

最后我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>nl.hu.v1ipass</groupId>
    <artifactId>barapp</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>barapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.27</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.26</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.26</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.json</artifactId>
            <version>1.0.4</version>
        </dependency>
    </dependencies>
</project>

我希望有人看到我在做错事情,谢谢。

2 个答案:

答案 0 :(得分:0)

一切看起来不错,值得怀疑的是服务器启动时。 您是否在跟踪中看到任何错误/异常可以帮助您进一步诊断?

答案 1 :(得分:0)

1。确保服务器在指定端口上正确启动。 2.确保您的端口上没有其他应用程序在运行 如果以上两个步骤都正常,则开始调试 3.通过将调试点放在方法的第一行中,确保执行点到达特定方法内部 4.逐行调试并检查是否缺少任何资源或路径