找不到url';Java中REST服务的s
有很多关于创建REST服务的帖子,我读过几十篇;我仍然无法得到我的服务。我的服务创建(通过PUT或POST)和返回(通过GET)Offerror,它们有id、名称和地址。Offer是一个类,Offerror是一个在ConcurrentMap中保存Offerror实例的类,OfferrorServlet是servlet类。请帮我理顺我的休息;我宁愿注释类,也不愿配置web.xml 为了运行,我将工件offerWebService-0.0.1-SNAPSHOT.war部署到JBoss。我通过邮递员进行测试,接受&内容类型为application/json集,并使用admin帐户进行身份验证 得到http://localhost:8080/OfferorWebService-0.0.1-SNAPSHOT/offerrors()生成“404未找到” 职位http://localhost:8080/OfferorWebService-0.0.1-SNAPSHOT/offerrors生成“405方法不允许” 我需要在servlet或调用中添加/更改什么?我需要额外的配置文件吗?谢谢 在pom.xml中找不到url';Java中REST服务的s,java,rest,servlets,Java,Rest,Servlets,有很多关于创建REST服务的帖子,我读过几十篇;我仍然无法得到我的服务。我的服务创建(通过PUT或POST)和返回(通过GET)Offerror,它们有id、名称和地址。Offer是一个类,Offerror是一个在ConcurrentMap中保存Offerror实例的类,OfferrorServlet是servlet类。请帮我理顺我的休息;我宁愿注释类,也不愿配置web.xml 为了运行,我将工件offerWebService-0.0.1-SNAPSHOT.war部署到JBoss。我通过邮递员进
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<build>
<sourceDirectory>src.main.java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
首先,我想你不能将这些注释与servlet结合使用,它们不是为它而设计的,我认为这不会正常工作,doGet、doPut、doTrace、doPost会很好地工作,如果你想使用java libs专注于rest服务,并且有更多的特性,你应该研究JAXR或jaxWS文档,howTo,Servlet更多地关注基本的http操作以及jsp和该模型,但是当然,您可以使用普通的Servlet,只需删除这些注释并给出所发生情况的反馈。谢谢。我让它工作了,我稍后会发布
@WebServlet("/Offerrors")
public class OfferorServlet extends HttpServlet {
static final long serialVersionUID = 1L;
static final ObjectMapper om = new ObjectMapper();
Offerors mapOfOfferors;
@Override
public void init() {
mapOfOfferors = new Offerors();
mapOfOfferors.setServletContext(this.getServletContext());
}
// GET /offeror or /offeror?id=123
@Override
@GET
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String param = request.getParameter("id");
Integer key = (param == null) ? null : Integer.valueOf((param.trim()));
// return an Offeror
try {
sendResponse(response, om.writeValueAsString(mapOfOfferors.getOfferror(key)));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
@POST // /offerors
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String address = request.getParameter("address");
String msg = "Creating an offeror";
if (name == null || address == null)
throw new RuntimeException(Integer.toString(HttpServletResponse.SC_BAD_REQUEST));
Offeror offeror = new Offeror();
// Save the ID of the newly created Offeror.
offeror.setName(name);
offeror.setAddress(address);
int id = mapOfOfferors.addOfferor(offeror);
try {
sendResponse(response, om.writeValueAsString(msg));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
// Longer PUT method removed for brevity
// Methods Not Allowed
@Override
public void doTrace(HttpServletRequest request, HttpServletResponse response) {
throw new RuntimeException(Integer.toString(HttpServletResponse.SC_METHOD_NOT_ALLOWED));
}
... (many more of the same)
}