我是java web服务的新手。使用jax-rs编写示例Web服务时,在堆栈跟踪中出现以下错误。运行一个服务器,将页面加载为http状态404.我需要编写简单的Web服务供我理解,然后我必须通过Android应用程序中的restful client访问Web服务。我在这里发布我的整个网络服务请帮助我。
请以正确的方式引导我。提前谢谢
Aug 26, 2014 6:55:04 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft\Web Platform Installer\;.
Aug 26, 2014 6:55:04 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webService' did not find a matching property.
Aug 26, 2014 6:55:04 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 26, 2014 6:55:04 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 26, 2014 6:55:04 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 699 ms
Aug 26, 2014 6:55:04 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 26, 2014 6:55:04 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Aug 26, 2014 6:55:06 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
webService
Aug 26, 2014 6:55:06 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class webService.myserviceClass
Aug 26, 2014 6:55:06 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Aug 26, 2014 6:55:06 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 03:28 PM'
Aug 26, 2014 6:55:07 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 26, 2014 6:55:07 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 26, 2014 6:55:07 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2082 ms
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>webService</display-name>
<servlet>
<servlet-name>Rest</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>webService</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
myserviceClass.java
package webService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// Plain old Java Object it does not extend as class or implements
// an interface
// The class registers its methods for the HTTP GET request using the @GET annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.
// The browser requests per default the HTML MIME type.
//Sets the path to base URL + /hello
@Path("/hello")
public class myserviceClass {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
答案 0 :(得分:2)
您必须访问的网址格式为:
http://your_hostname:port-number/application-context-name/rest/path-name-of-your-web-service
当应用程序在您的计算机上运行时, <hostname>
为localhost
<port-number>
是8080
- tomcat服务器的默认端口
<application-context-name>
是部署在tomcat服务器中的应用程序的名称,如果您将其命名为webService
,则应用程序上下文名称将为webService
rest
- 这是在你的web.xml文件中配置的jersey Servlet的名称,根据你的xml rest
<path-name-of-your-web-service>
- 根据您的java网络服务代码,这将是/hello
。
请尝试:
http://localhost:8080/webService/rest/hello
答案 1 :(得分:0)
我在tomcat日志中没有看到任何错误。我认为没有处理请求的Web服务的servlet。 (控制web.xml)或者您正在尝试访问错误的上下文。