泽西休息服务使用Json annotaion无法正常工作

时间:2016-09-16 14:25:19

标签: java json rest api jersey

我有以下课程: ServerStartup.java

import java.io.IOException;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.net.httpserver.HttpServer;

public class ServerStartUp {
static final String BASE_URI = "http://localhost:9999/";
public static void main(String[] args) {


    try {
        final ResourceConfig rc = new PackagesResourceConfig("com.twins.engine");
        rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);         
        HttpServer server = HttpServerFactory.create(BASE_URI,rc);
        server.setExecutor(null);
        server.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

和以下类SysInfo.Java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.twins.engine.model.SysInfo;

@Path("/Sys")
public class General {

@GET
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)
public SysInfo getInfo(){
    SysInfo info = new SysInfo("Monetoring 365");
    return info;
}
}

和以下模型类SysInfo.java

public class SysInfo {
private String name;
public SysInfo(String name) {
    this.setName(name);
}

public String getName() {return name;}

public void setName(String name) {this.name = name; }
}

当我运行serverStartup类并导航到以下url时: http://127.0.0.1:9999/sys/info

我得到以下例外:

引起:com.sun.jersey.api.MessageException:Java类com.twins.engine.model.SysInfo的消息体编写器,Java类类com.twins.engine.model.SysInfo和MIME媒体找不到类型text / html     ......还有15个

enter image description here我也将以下Jars附加为图像,是否有任何缺少的配置我必须做

1 个答案:

答案 0 :(得分:0)

尝试以下

create index on campaign_list (notes, user_id, send_date)

可能你必须添加jackson-mapper



final ResourceConfig rc = new PackagesResourceConfig("com.twins.engine");
final Map<String, Object> config = new HashMap<String, Object>();
config.put("com.sun.jersey.api.json.POJOMappingFeature", true);
rc.setPropertiesAndFeatures(config);
HttpServer server = HttpServerFactory.create(BASE_URI,rc);
server.setExecutor(null);
server.start();
&#13;
&#13;
&#13;