我必须运行java URL =" http:// localhost:8080 / RESTfulExample / rest / json / metallica / get"在Apache Tomcat上,但我得到404错误。我想得到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();
}
}
}
例外:
java.lang.RuntimeException:失败:HTTP错误代码:404 at com.mkyong.client.JerseyClientPost.main(JerseyClientPost.java:24)
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>
答案 0 :(得分:0)
当路径应为http://localhost:8080/RESTfulExample/rest/json/product/get;
时,您正在查询http://localhost:8080/RESTfulExample/rest/json/metallica/get答案 1 :(得分:0)
您的问题未显示使用/RESTfulExample/rest
作为基本路径的指示
因此,假设应用程序的名称为RESTfulExample
,您似乎正在为此路径服务:
/RESTfulExample/json/metallica/get
此外,您的代码中的两个**应该有编译错误:
**track.setTitle("Enter Sandman");