REST的新手。这是我的两个班级:
values.java
string foo = ConvertFromDBVal<string>(record.GetValue(A));
sum.java
@Path("/values")
public class values {
int value = 0;
List<Integer> list = new ArrayList<Integer>();
@GET
@Produces(MediaType.TEXT_PLAIN)
public int getSum() {
return sum.sum(list);
}
@POST
public String addValue() {
list.add(value);
return "ok";
}
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public String deleteList() {
list.clear();
return "ok";
}
}
的web.xml
@Path("/values/sum")
public class sum {
public static int sum(List<Integer> numbers) {
int sum = 0;
numbers.add(10);
for(int i : numbers) {
sum += i;
}
return sum;
}
}
当我访问我的Tomcat服务器时,它只发送10作为响应(如果POST不工作,这是正确的)。有关新手的任何提示吗?我尝试通过http://localhost:8080/PACKAGENAME/api/values?value=15并且每次只发回10。我认为它只使用GET方法。我如何进行POST?
谢谢!
更新1:
<?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" id="WebApp_ID" version="3.0">
<display-name>PACKAGENAME</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>PACKAGENAME</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
更新2:我如何将值(?值= 15)从POST传递到我的列表?
答案 0 :(得分:1)
当您通过http://localhost:8080/PACKAGENAME/api/values?value=15时,您当然会调用GET,因为您在URL中传递了带有值的GET参数。如果要调用POST方法,请使用Postman作为示例。