简体   繁体   English

从HTML调用REST Web服务

[英]Calling REST web service from HTML

package com.leadwinner;

import javax.ws.rs.GET;  
import javax.ws.rs.POST;
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.Produces;  
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;  

@Path("ServiceProvider2")  
public class ServiceProvider2 {  
 @GET  
 @Path("/InchToFeet")  
  @Produces(MediaType.TEXT_XML)  
  public String convertInchToFeet(@QueryParam("i") int i) {  

    int inch=i;  
    double feet = 0;  
    feet =(double) inch/12;  

    return "<InchToFeetService>"  
    + "<Inch>" + inch + "</Inch>"  
      + "<Feet>" + feet + "</Feet>"  
     + "</InchToFeetService>";  
  }  


}

The following URL returns a result as XML: 以下URL以XML格式返回结果:

.../SampleRest/ServiceProvider2/InchToFeet?i=2 ... / SampleRest / ServiceProvider2 / InchToFeet?I = 2

This is fine, though when I am trying to run HTML files using tomcat server, I get the error: 很好,但是当我尝试使用tomcat服务器运行HTML文件时,出现错误:

webpage not found 404 html files not running in restful web services using jersey 找不到使用jersey的网页404 html文件未在静态Web服务中运行

In order to make html files also work, you should modify the web.xml file to map your Jersey Servlet only to the urls that it should handle (RESTful urls). 为了使html文件也能工作,您应该修改web.xml文件以将Jersey Servlet仅映射到它应处理的网址(RESTful网址)。

<web-app>
    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            ...
        </init-param>
    </servlet>
    ...
    <servlet-mapping>
        <servlet-name>MyApplication</servlet-name>
        <url-pattern>/ServiceProvider2/*</url-pattern>
    </servlet-mapping>
    ...
</web-app>

More info here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM