gpt4 book ai didi

java - Jersey Rest 服务无法使用 Json 注释工作

转载 作者:行者123 更新时间:2023-11-30 07:08:55 24 4
gpt4 key购买 nike

我有以下类(class):服务器启动.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 类并导航到以下网址时: 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我还附加了以下 jar 作为图像,是否有任何缺少的配置我必须做

最佳答案

尝试以下

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();

也许你必须添加 jackson-mapper

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>

关于java - Jersey Rest 服务无法使用 Json 注释工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39533929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com