Java 2中的Web服务-提供REST资源

Hi, welcome back. Hopefully everything we did in part 1 worked out for you. If anything didn't please leave us a comment and we will look into it as soon as possible.

在本文中,我们将学习REST处理程序在Java中的外观以及如何告诉服务器已存在。

How are REST Resources structured and how will we access them?

通常,构造Java应用程序的常用方法是将操作分为逻辑组。 例如。 以“用户”为目标的所有呼叫将被分组为一个名为UsersController。 通常,最常见的HTTP请求方法与具体的操作类型相关联:

  • GET-获取数据POST-创建数据PUT / PATCH-更新数据删除-销毁数据

当然,还有许多其他请求方法,但我们将主要关注这四种方法,因为它们几乎提供了我们小型项目所需的所有功能。

在这里,我们可以看一个小的示例控制器:

    @Path("")
    public class ExampleResource {
        @GET
            @Produces(MediaType.TEXT_HTML)
        public String hello() {
            return "<h1>Hello World</h1>";
        }

            @GET
            @Path("test")
            @Produces(MediaType.APPLICATION_JSON)
            public String test() {
                return "{ \"Hello\": \"World\"}";
            }
    }

的@路径 Annotation tells the server the base path the controller is operating at. This annotation can also be put on top of the handler Method. 的path this method is listening at will then be created by joining the strings from the annotation on the class and on the method using a/

每个应该处理请求的方法,也将获得其支持的HTTP方法名称的注释。 在这个小例子中,我们的两种方法仅支持GET。

Tell the server what we want and what we give

With @Produces and @Consumes we tell the server what type of content our method wants and what type of content it will return back to the consumer. In the Javax.ws.rs specification, there is a Enum provided that's called MediaType. It contains all the media types supported by Java. To serve HTML, we will pass MediaType.TEXT_HTML to the @Produces annotation. If we want to get some JSON from the request, we just tell the server in the @Consumes annotation with MediaType.APPLICATION_JSON.

Pass input with the request

很好,所有这些,但是我们如何检查请求中的输入是否很酷?

没关系 我们只是为其创建一个类:

    public class MyFancyInput 
        private String username;
        private Integer age;
        private Boolean acceptTOS;

        public String getUsername() {
            return this.username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public Integer getAge() {
            return this.age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Boolean getAcceptTOS() {
            return this.acceptTOS;
        }

        public void setAcceptTOS(Boolean acceptTOS) {
            this.acceptTOS = acceptTOS;
        }
    }

请求主体对象只是普通的Java POJO。 所有属性必须具有getter和setter方法,因为反序列化将尝试调用它们。 为了确保null字段不会破坏我们的服务,我们仅使用非原始数据类型。 这是因为布尔值例如不支持空类型,但布尔型做。

输入完类后,我们将告诉操作我们希望将其作为参数:


    @Path("")
    public class ExampleResource {
        @GET
            @Produces(MediaType.TEXT_HTML)
        public String hello() {
            return "<h1>Hello World</h1>";
        }

            @GET
            @Path("test")
            @Produces(MediaType.APPLICATION_JSON)
            public String test() {
                return "{ \"Hello\": \"World\"}";
            }

            @POST
            @Path("test")
            @Consumes(MediaType.APPLICATION_JSON)
            @Produces(MediaType.APPLICATION_JSON)
            public MyFancyInput getInput(@NotNull MyFancyInput input) {
                return input;
            }
    }

在这里,我们只是获取输入,然后再次将其返回给客户端。 随着@NotNull注解,我们告诉服务器此方法的参数是必需的,不能为null。

如果输入来自请求中的特殊位置,我们将使用正确的注释对其进行注释。 可用的注释有:

  • @BeanParam@CookieParam@FormParam@PathParam@QueryParam@MatrixParam

Link it all together

We have the resource, we have the data structure, and if you did Part 1, you also have the server. Now we need to connect it all:

在我们的主类中,我们只向ResourceConfig添加一些内容:

    // Holds all the resources we want to register (currently, there's nothing to do)
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(ExampleResource.class); // our rest resource
    // the parser for JSON and XML request bodies
    resourceConfig.register(JacksonFeature.class);

Now when we run our server, we should be able to reach the resources we just create at localhost:8080/and on localhost:8080/test

完成所有这些之后,我们的项目结构应如下所示:

src
- main
    - java
        - Main.java
        - ExampleResource.java
        - MyFancyInput.java
- test

希望您玩得开心,一切都为您做好。 祝你有美好的一天! :D

from: https://dev.to//funcke/web-services-in-java-2-provide-rest-resources-546p

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值