如何在Jersey Restful webservice上输出JSON格式

时间:2018-01-07 08:54:35

标签: java json rest jersey jax-ws

我已将Jersey库添加到我的应用程序以及媒体JSON库中,如您所见:

    <?xml version="1.0" encoding="UTF-8"?>
<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>click-digital-backend</groupId>
    <artifactId>ClickDigitalBackend</artifactId>
    <version>1.0-SNAPSHOT</version>



    <dependencies>



        <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>



        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <scope>runtime</scope>
            <version>2.22.1</version>
        </dependency>





    </dependencies>



</project>

这里我有一个简单的测试方法,可以将列表作为JSON返回:

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Path("/devmanager")
public class DeviceManager implements DevManager {
@GET
@Produces({MediaType.APPLICATION_JSON})
public MyList getString(){
    List<String> al = new ArrayList();
    al.add ("Hello");
    al.add ("World");
    MyList list = new MyList (al);

    return list;
}

@Path("/string")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getS(){
    return "Hallo";
}

package devicemanager;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "MyList")
public class MyList {

    List<String> userList;

    public MyList (List<String> lst)
    {
        this.userList = lst;
    }

    //setters and getters goes here
}

现在我正在运行glassfish服务器v.4.1.1,但我在浏览器和日志中收到500错误:

[2018-01-07T16:51:38.646+0100] [glassfish 4.1] [SEVERE] [] [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] [tid: _ThreadID=34 _ThreadName=http-listener-1(4)] [timeMillis: 1515340298646] [levelValue: 1000] [[
  MessageBodyWriter not found for media type=application/json, type=class devicemanager.MyList, genericType=class devicemanager.MyList.]]

我已经搜索了几个小时的解决方案并尝试包含许多不同的库,这些库应该执行此JSON格式化但我找不到解决方案。即使我请求XML格式,我也会遇到同样的错误。 如果有人能帮我找到那个很棒的问题!

1 个答案:

答案 0 :(得分:0)

您无法将回复JSON定义为List,因为JAXB无法识别@XmlRootElementjava.util.List上的java.util.ArrayList课程定义。

我建议将List包裹在后面

@XmlRootElement(name = "MyList")
public class MyList {

    @XmlElement(required=true) 
    List<String> userList;

    public MyList (List<String> lst)
    {
       this.userList = lst;
    }

    //setters and getters goes here
}

并且,在您的服务实现中

@GET
@Produces({MediaType.APPLICATION_JSON})
public MyList getString(){
    List<String> al = new ArrayList<>();
    al.add ("Hello");
    al.add ("World");
    MyList list = MyList (al);
    return list;
}

此外,最好在声明任何集合而不是原始类型时使用泛型,以避免任何运行时异常。

ArrayList al = new ArrayList();

应该是

ArrayList<String> al = new ArrayList<>();

<强> ==被修改==

你可以添加

  <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.10</version>
  </dependency>

删除

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <scope>runtime</scope>
    <version>2.22.1</version>
</dependency>