gpt4 book ai didi

jersey-2.0 - Jersey 在尝试返回 XML 响应时返回 500

转载 作者:行者123 更新时间:2023-12-03 22:45:13 25 4
gpt4 key购买 nike

我正在尝试使用基于 this 的 Jersey 2.12 创建我自己的 RESTful WS 应用程序文章。我想根据从 url 传递的 id 返回一个类的 XML 表示,但是,从高级 Rest 客户端应用程序(谷歌浏览器应用程序)或浏览器尝试时,我收到了 500 响应代码。以下是详细信息:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-
app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WS_RESTful_Practice</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

TestRestModel.java
package test.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class TestRestModel{

/**
*
*/
private static final long serialVersionUID = -8391589100962515747L;
private String name;
private String content;

public TestRestModel(String name, String content){
this.name = name;
this.content = content;
}

public String getName() {
return name;
}

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

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

TestResource.java
package test.services;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import test.dao.TestModelDao;
import test.model.TestRestModel;

@Path("/test")
public class TestResource {

@GET
@Path("{id}")
public Response getModel(@PathParam("id") String id){
return Response.ok().entity(TestModelDao.instance.getModel().get(id)).build();
}
}

TestModelDao.java
package test.dao;

import java.util.HashMap;
import java.util.Map;
import test.model.TestRestModel;

public enum TestModelDao {
instance;

private Map<String, TestRestModel> container = new HashMap<String, TestRestModel>();

private TestModelDao(){

TestRestModel model = new TestRestModel("a", "this is first");
container.put("1", model);
model = new TestRestModel("b", "this is second");
container.put("2", model);
model = new TestRestModel("c", "this is third");
container.put("3", model);
}

public Map<String, TestRestModel> getModel(){
return container;
}
}

我对 Jersey 和 REST 完全陌生。而且我还不知道如何从 Jersey 记录错误。

最佳答案

当您没有为 JAXB bean 提供默认的无参数构造函数时,就会发生这种情况。因此,在您的情况下,您应该通过添加一个来修改类:

  public TestRestModel(){

}

这是由于 JSR-222 中的要求所致:

existing types, authored by users, are required to provide a no arg constructor. The no arg constructor is used by an unmarshaller during unmarshalling to create an instance of the type.

关于jersey-2.0 - Jersey 在尝试返回 XML 响应时返回 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26014184/

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