[英]Run application in apache localhost:8080
I have to run java URL="http://localhost:8080/RESTfulExample/rest/json/metallica/get" on Apache Tomcat but i am getting 404 Error. 我必须在Apache Tomcat上运行java URL =“ http:// localhost:8080 / RESTfulExample / rest / json / metallica / get”,但我遇到404错误。 i want to get json response so what am i doing? 我想得到json响应,所以我在做什么?
@Path("/json/metallica")
public class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
return track;
}
public class NetClientGet {
public static void main(String[] args) {
try {
URL url = new URL(
"http://localhost:8080/RESTfulExample/rest/json/metallica/get");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Exception : 例外情况:
java.lang.RuntimeException: Failed : HTTP error code : 404 at com.mkyong.client.JerseyClientPost.main(JerseyClientPost.java:24) java.lang.RuntimeException:失败:HTTP错误代码:com.mkyong.client.JerseyClientPost.main(JerseyClientPost.java:24)上的404
Web.xml : Web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/</url-pattern>
</servlet-mapping>
</web-app>
Your questions shows no indication of using /RESTfulExample/rest
as base path. 您的问题没有表明使用/RESTfulExample/rest
作为基本路径。
So, assuming the name of the application is RESTfulExample
it seems you're serving this path: 因此,假设应用程序的名称是RESTfulExample
,似乎您正在使用以下路径:
/RESTfulExample/json/metallica/get
Also, you should have a compilation error from the two ** in your code: 另外,您应该在代码中的两个**处出现编译错误:
**track.setTitle("Enter Sandman");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.