小编Cod*_*Bad的帖子

在 RESTEasy 中使用 POST 请求

早些时候,我使用 POSTMAN 工具提交 GET/PUT/POST/DELETE,但现在我尝试在不使用任何 REST API 客户端的情况下实现 POST 操作。为此我参考了 这个网站。我创建了一个 Maven 项目,这些是我的示例代码:

网络.xml

<?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>RESTEasyJSONExample</display-name>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<!-- Auto scan REST service -->
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

<!-- this should be the same URL pattern as the servlet-mapping property -->
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>
Run Code Online (Sandbox Code Playgroud)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java.resteasy</groupId>
  <artifactId>RESTEasyJSONExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>

<repositories>
    <repository>
        <id>JBoss …
Run Code Online (Sandbox Code Playgroud)

java rest json resteasy maven

5
推荐指数
1
解决办法
3万
查看次数

通过对象实例访问静态方法

我已经了解到,如果我们希望调用另一个类的静态方法,那么你必须在调用静态方法时编写类名.在下面的程序中,我在Employee_Impl类中创建了Employee类的对象,并使用该对象,我仍然可以访问该count方法.count如果static只使用类名访问方法,为什么它允许我在对象中使用方法?这是否意味着可以使用对象和类名来访问静态方法?

Employee.java

public class Employee{
    static int counter = 0;
    static int count(){
        counter++;
        return counter;
    }
}
Run Code Online (Sandbox Code Playgroud)

Employee_Impl.java

class Employee_Impl
    public static void main(String args[]){
        Employee obj = new Employee();
        System.out.println(obj.count());
        System.out.println(Employee.count());
        System.out.println(obj.count());        
    }
}
Run Code Online (Sandbox Code Playgroud)

output

1
2
3
Run Code Online (Sandbox Code Playgroud)

java static-methods

3
推荐指数
2
解决办法
1287
查看次数

标签 统计

java ×2

json ×1

maven ×1

rest ×1

resteasy ×1

static-methods ×1