在Java中找不到REST服务的网址

问题描述

关于创建REST服务的帖子很多,我读了很多。我仍然无法获得服务。我的服务创建(通过PUT或POST)并返回(通过GET)Offerror,它们具有ID,名称和地址。 Offer是一个类,Offerrors是一个在ConcurrentMap中保存Offerror实例的类,而OfferrorServlet是servlet类。请帮助我理顺我的REST编码;我宁愿为类添加注释,也不想配置web.xml。

要运行,我将工件OfferorWebService-0.0.1-SNAPSHOT.war部署到JBoss。我通过PostMan进行了测试,并设置了application / json的Accepting&Content-type并使用管理员帐户进行了身份验证

获取http:// localhost:8080 / OfferorWebService-0.0.1-SNAPSHOT / offerrors()会产生“找不到404

POST http:// localhost:8080 / OfferorWebService-0.0.1-SNAPSHOT / offerrors产生“ 405方法不允许”

我需要在servlet或调用添加/更改什么?我是否需要其他配置文件?谢谢!

在pom.xml中

    <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>

OfferrorServlet.java(为了简洁起见,我已删除了已删除代码

@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)
}

解决方法

首先,我想您不能将这些批注与servlet结合使用,它们不是为此而设计的,并且我认为它将无法正常工作,如果您想使用,doGet,doPut,doTrace,doPost会很好地完成工作。 Java库专注于具有更多功能的REST服务,您应该研究有关jaxRS或jaxWS文档,howTo的知识,servlet更专注于基本的http迭代以及jsp和此模型,但是您当然可以使用简单的servlet,只需删除这些注释即可并提供所发生情况的反馈。