从index跳转JSP页面遇jakarta.ws.rs.NotFoundException(404)求助
问题描述
在Apache Tomcat 9服务器上运行RESTfulWSDemo项目时,可正常访问首页http://localhost:1040/RESTfulWSDemo/及index.html内容,但跳转至JSP页面时出现jakarta.ws.rs.NotFoundException: HTTP 404 Not Found错误。此前曾遇到java.lang.IllegalStateException: InjectionManagerFactory not found错误,已通过添加HK2依赖解决,目前服务似乎无法被正确找到,期望获取明文响应"stuff"。
POM依赖
<dependencies> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>3.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>3.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 --> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>3.1.1</version> </dependency> </dependencies>
Client类
package com.estore.test; import java.net.URI; import org.glassfish.jersey.client.ClientConfig; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.ClientBuilder; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.UriBuilder; public class TestClient { WebTarget target; public TestClient() { ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); URI uri = UriBuilder.fromUri("http://localhost:1040/RESTfulWSDemo").build(); target = client.target(uri); } public String getPlainResponse() { String response = target.path("rest").path("user").request().accept(MediaType.TEXT_PLAIN).get(String.class); return response; } }
Servlet映射(web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>RESTfulWSDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>MyRESTServlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.estore.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyRESTServlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
JSP页面
<%@page import="com.estore.test.TestClient"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>My REST Client</title> </head> <body> <h3> Testing RESTful Service</h3> <%! TestClient client = new TestClient(); %> Plain Response: <%= client.getPlainResponse() %> <br> </body> </html>
Service类
package com.estore.rest; import java.util.Date; //import org.glassfish.jersey.servlet.ServletContainer; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; @Path("/user") public class UserService { @GET @Produces(MediaType.TEXT_PLAIN) public String registerUserWithPlainResponse() { String response = "stuff"; return response; } }
内容的提问来源于stack exchange,提问作者Charanjot Singh
相关产品推荐
相关产品推荐

