以下是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_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>pgwrapper</display-name>
<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>com.example.postgres.wrapper</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
以下是我的ServiceClassProvider.java文件:
@SuppressWarnings({ "rawtypes", "unchecked" })
@Path("/pg")
public class PostgresDBServiceProvider {
static final String HOST = "select * from host";
/**
* @return All Host Ids
* @throws Exception
*/
@GET
@Path("/getAllHostIds")
@Produces({ MediaType.APPLICATION_XML })
public static String getAllHostIds() throws Exception {
List<String> list = new ArrayList<>();
String hostId = null;;
ListWrapper listWrapper = new ListWrapper();
Statement st = PostgresDBConnector.getInstance().createStatement();
ResultSet rs = st.executeQuery(HOST);
while (rs.next()) {
hostId = rs.getString("host_id");
break;
}
return hostId;
}
Client.java:
public class PostgresClient {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
.......................
Client client =
ClientBuilder.newBuilder().withConfig(config)
.hostnameVerifier(new InsecureHostnameVerifier())
.sslContext(ctx).build();
WebTarget target = client.target(getBaseURI());
String hostIp =
target.path("rest")
.path("getAllHostIds").request()
.accept(MediaType.TEXT_PLAIN).get(String.class);
System.out.println("hostIp:" + hostIp);
}
private static URI getBaseURI() {
return UriBuilder.fromUri("https://192.168.100.108:8443/pg").build();
}
}
在运行客户端时,我收到以下错误:
Exception in thread "main" javax.ws.rs.NotFoundException: HTTP 404 Not
Found
at
org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1020)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:819)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
我的Tomcat服务器在8443上运行,但是我无法执行这个休息端点,不知道这里错过了哪个配置。需要继续指点。