简体   繁体   English

使用Jersey的Java中的Hashmap示例?

[英]Hashmap Example in Java using Jersey?

I am currently working with with web services for the first time and I have to write some examples of different web services in Jersey using Maven, 我目前是第一次使用Web服务,我必须使用Maven在Jersey编写一些不同Web服务的示例,

But I can only seem to get it to work in Spark (Here's My spark example) 但我似乎只能在Spark中使用它(这是我的Spark示例)

//Hashmap Example
get("add/:num1/:num2", new Route() {

    @Override
    public Object handle(Request rqst, Response rspns) throws Exception {
        rspns.type("application/json");

        int num1 = Integer.parseInt(rqst.params(":num1"));
        int num2 = Integer.parseInt(rqst.params(":num2"));
        HashMap<String, Integer> map = new HashMap<>();
        map.put("result", num1+num2);
        Gson gson = new Gson();
        return gson.toJson(map);
    }
});

I just need some help writing it for a Jersey example ? 我只需要一些帮助来编写Jersey示例?

Any help you can offer would be great 您可以提供的任何帮助都会很棒

First, with Jersey you need to register a JSonProvider, so that you can return an Object and it will be serialized for you in JSON. 首先,在Jersey上,您需要注册一个JSonProvider,以便您可以返回一个Object并将其以JSON序列化。 You can alternatively support XML with no more effort. 您可以轻松地支持XML。

Personally I use Jackson. 我个人使用杰克逊。 The default with Jersey is Moxy, which doesn't support Maps, and has "issues" even with simple List (you need to wrap them) see this post 与新泽西默认是莫西,不支持地图,并且有“问题”,甚至用简单的列表(你需要用它们)看到这个帖子

Configuring Jersey with Jackson : https://jersey.java.net/documentation/latest/media.html#json.jackson 用Jackson配置Jersey: https : //jersey.java.net/documentation/latest/media.html#json.jackson

Moxy issue with Map : How to return a JSON object from a HashMap with Moxy and Jersey Map的Moxy问题: 如何使用Moxy和Jersey从HashMap返回JSON对象

Here's a working example of a Jersey web service, returning a Map, given you are using Jackson 如果您使用杰克逊,这是一个泽西岛Web服务的工作示例,返回一个Map

@GET
@Path("add/{num1}/{num2}")
@Produces(MediaType.APPLICATION_JSON)
public Response getClubNames(@PathParam("num1") Integer num1, @PathParam("num2") Integer num2) {

    Map<String, Object> returnMap = new HashMap<String, Object>();

    returnMap.put("resultAsString", Integer.toString(num1 + num2));
    returnMap.put("resultAsInt", num1 + num2);

    return Response.status(Response.Status.OK).entity(returnMap).build();

}

Personal note, as you are starting to use Jersey : 在开始使用Jersey时的个人说明:

I really like Jersey. 我真的很喜欢球衣。 But there is a serious hard-coupling issue with HK2. 但是HK2存在严重的硬耦合问题。 https://java.net/jira/browse/JERSEY-1933 . https://java.net/jira/browse/JERSEY-1933 This is out of scope, but you should understand what this means before choosing a JAX-RS 2.0 implementation. 这超出了范围,但是在选择JAX-RS 2.0实现之前,您应该了解这意味着什么。 In a stand alone Web Application, Jersey works like a charm. 在独立的Web应用程序中,Jersey的工作原理就像是一种魅力。

Figured it out myself 我自己想通了

//HashMap Example
@GET
@Path("add/{num1}/{num2}")
public String Hashmap(@PathParam("num1") int num1, @PathParam("num2") int num2){
          HashMap<String, Integer> map = new HashMap<>();
          map.put("result", num1+num2);
          Gson gson = new Gson();
          return gson.toJson(map);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM