0

我刚刚开始spring framework 5.2.8jersey 3.0.2版本集成。我知道我们可以用 spring-boot 做到这一点,但这不是我的要求。我希望该服务在 Tomcat 服务器 8.5 上运行。我已经定制了pom.xml微型尺寸,左侧是 groupId 其余的是依赖 artifactIds :

org.springframework             : spring-core, spring-context, spring-beans
org.glassfish.jersey.ext        : jersey-spring5, spring-web
org.glassfish.jersey.containers : jersey-container-servlet, jersey-container-grizzly2-http
org.glassfish.jersey.media      : jersey-media-json-jackson
com.fasterxml.jackson.core      : jackson-core, jackson-annotations, jackson-databind
com.fasterxml.jackson.module    : jackson-module-jaxb-annotations

我的主要课程看起来像:

@ApplicationPath("resources")
public class Application extends ResourceConfig {

    public Application() {
        packages("some.path.of.it.resources;some.other.path.of.it.resources");
    }
}

现在是我的主要资源类:

@Path("helloworld")
public class MyResources {

     public static final String CLICHED_MESSAGE = "Hello Krishna ... !";
     
    @GET
    @Produces("text/plain")
        public String getHello() {
            return CLICHED_MESSAGE;
        }
}

现在我有几个问题:

  1. http://localhost:8080/warname/resources/helloworld 正在抛出 404 为什么会这样?
  2. 我无法使用 Spring 的 MediaType 类,但被迫使用 jakarta 包我该如何更改它?
  3. 如果我想将杰克逊绑定与我的资源输入对象一起使用,我该怎么做?

我看过一些在线示例,但最终出现了一些其他错误。

更新

两者都尝试了http://localhost:8080/hc-cms-1.0-SNAPSHOT/resources/helloworldhttp://localhost:8080/resources/helloworld只是在日志中没有引发HTTP Status 404 – Not Found其他错误或问题。

20-May-2021 16:36:18.543 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/8.5.66
20-May-2021 16:36:18.547 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          May 8 2021 22:44:01 UTC
20-May-2021 16:36:18.547 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.66.0
20-May-2021 16:36:18.548 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
20-May-2021 16:36:18.548 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
20-May-2021 16:36:18.548 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
20-May-2021 16:36:18.548 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Java\jdk1.8.0_271\jre
20-May-2021 16:36:18.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_271-b09
20-May-2021 16:36:18.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
20-May-2021 16:36:18.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Tools\apache-tomcat-8.5.66
20-May-2021 16:36:18.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         C:\Tools\apache-tomcat-8.5.66
20-May-2021 16:36:18.549 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Tools\apache-tomcat-8.5.66\conf\logging.properties
20-May-2021 16:36:18.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
20-May-2021 16:36:18.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
20-May-2021 16:36:18.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
20-May-2021 16:36:18.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
20-May-2021 16:36:18.550 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Tools\apache-tomcat-8.5.66
20-May-2021 16:36:18.558 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Tools\apache-tomcat-8.5.66
20-May-2021 16:36:18.563 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Tools\apache-tomcat-8.5.66\temp
20-May-2021 16:36:18.566 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_271\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Python39\Scripts\;C:\Python39\;C:\Windows;C:\Windows\system32;C:\Program Files\Java\jdk1.8.0_271\bin;C:\Program Files\Docker\Docker\Resources\bin;C:\maven\bin;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\TortoiseGit\bin;D:\software\apache-ant-1.8.4\bin;C:\Program Files\PuTTY\;C:\Users\rahul.kishan\Downloads\sonar-scanner-cli-3.0.3.778-windows\sonar-scanner-3.0.3.778-windows\bin;D:\software\kotlin-compiler-1.2.21\kotlinc\bin;%M2_HOME%\bin;C:\Program Files (x86)\CrSSL\bin;C:\gradle\bin;C:\apache-tomcat\apache-tomcat-8.5.0\bin;C:\Platform\UnxUpdates\;C:\Program Files\PostgreSQL\bin;C:\ProgramData\chocolatey\bin;C:\Program Files\MariaDB 10.5\bin;C:\Users\raghuveer.murthy\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\Git\cmd;C:\DB\mysql-5.6.51\bin;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Users\raghuveer.murthy\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin;;C:\Users\raghuveer.murthy\AppData\Roaming\npm;C:\Users\raghuveer.murthy\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\raghuveer.murthy\AppData\Local\Programs\Microsoft VS Code\bin;.]
20-May-2021 16:36:18.795 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
20-May-2021 16:36:19.291 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
20-May-2021 16:36:19.315 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1407 ms
20-May-2021 16:36:19.374 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
20-May-2021 16:36:19.375 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.66]
20-May-2021 16:36:19.423 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\Tools\apache-tomcat-8.5.66\webapps\hc-cms-1.0-SNAPSHOT.war]
20-May-2021 16:36:23.155 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
20-May-2021 16:36:23.242 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Tools\apache-tomcat-8.5.66\webapps\hc-cms-1.0-SNAPSHOT.war] has finished in [3,818] ms
20-May-2021 16:36:23.245 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\docs]
20-May-2021 16:36:23.289 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\docs] has finished in [44] ms
20-May-2021 16:36:23.289 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\examples]
20-May-2021 16:36:23.849 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\examples] has finished in [560] ms
20-May-2021 16:36:23.851 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\host-manager]
20-May-2021 16:36:23.909 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\host-manager] has finished in [58] ms
20-May-2021 16:36:23.911 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\manager]
20-May-2021 16:36:24.071 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\manager] has finished in [160] ms
20-May-2021 16:36:24.073 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\ROOT]
20-May-2021 16:36:24.118 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Tools\apache-tomcat-8.5.66\webapps\ROOT] has finished in [45] ms
20-May-2021 16:36:24.149 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
20-May-2021 16:36:24.218 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 4902 ms
4

1 回答 1

0

http://localhost:8080/manager/html

你看到你的应用部署在那里了吗?

------------- 已编辑 ----------------

你有 servlet 配置设置吗?

@Order(Ordered.HIGHEST_PRECEDENCE)
public class ApplicationInitializer 
  implements WebApplicationInitializer {
 
    @Override
    public void onStartup(ServletContext servletContext) 
      throws ServletException {
 
        AnnotationConfigWebApplicationContext context 
          = new AnnotationConfigWebApplicationContext();
 
        servletContext.addListener(new ContextLoaderListener(context));
        servletContext.setInitParameter(
          "contextConfigLocation", "com.yourpackage.server");
    }
}

@ApplicationPath("/resources")
public class ApplicationConfig extends Application {
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(
          Arrays.asList(
            MyResources.class));  <-- Your MySources class that has the @GET
    }
}

@Path("helloworld")
public class MyResources {

     public static final String CLICHED_MESSAGE = "Hello Krishna ... !";
     
    @GET
    @Produces("text/plain")
    public String getHello() {
            return CLICHED_MESSAGE;
     }
}

然后尝试“http://localhost:8080/yourwarname/resources/helloworld”;

于 2021-05-20T09:19:07.247 回答