<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>helloworlddisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>JAX-RS REST Servletservlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainerservlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packagesparam-name>
<param-value>com.helloworldparam-value>//这里要写自己的存放controller的package
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servletservlet-name>
<url-pattern>/rest/*url-pattern>
servlet-mapping>
web-app>
3.编写前端接受参数
package helloworld;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
public class Demo {
@GET
@Path("{name}")
@Produces(MediaType.APPLICATION_JSON)//可以返回json,根据前端需求编写
public String show(@PathParam("name")String name){
String str = "{name:\"hello\"}";
System.out.println(name);
return str;
}
}
4.访问
调用 http:localhost:8080/项目名/rest/helloworld
就可以在后台接收到helloworld