jjzjj

java - Glassfish 4 REST XML 工作 JSON 错误

coder 2024-06-25 原文

我在 Glassfish 4.1 上有一个 Web 服务,它对 XML 正常工作,但对 JSON 不正常。

实体类是:

@XmlRootElement
public class Person implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -8969081094076790550L;
    Integer id;
    String firstName;
    String lastName;
    String employeeId;

    /**
     * 
     */
    public Person() {
    }

    @Override
    public String toString() {
        return firstName + " " + lastName + " [" + employeeId + "] [id: " + id + "]";
    }
    /**
     * @return the firstName
     */
    public String getFirstName() {
        return this.firstName;
    }
    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    /**
     * @return the lastName
     */
    public String getLastName() {
        return this.lastName;
    }
    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    /**
     * @return the employeeId
     */
    public String getEmployeeId() {
        return this.employeeId;
    }
    /**
     * @param employeeId the employeeId to set
     */
    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    /**
     * @return the id
     */
    public Integer getId() {
        return this.id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

}

相关服务代码为:

@GET
@Path("xml")
@Produces(MediaType.TEXT_XML)
public Collection<Person> getPeopleXML() {
    return personDao.getAllPeople(); 
}

@GET
@Path("json")
@Produces(MediaType.APPLICATION_JSON)
public Collection<Person> getPeopleJSON() {
    return personDao.getAllPeople(); 
}

XML 调用有效,我得到:

<people>
 <person>
  <employeeId>2234</employeeId>
  <firstName>Mike</firstName>
  <id>2</id>
  <lastName>Jones</lastName>
 </person>
 <person>
  <employeeId>22314</employeeId>
  <firstName>Joe</firstName>
  <id>4</id>
  <lastName>Smith</lastName>
 </person>
</people>

我在 JSON 调用中遇到错误:

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector root cause

org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector root cause

java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector root cause

java.lang.ClassNotFoundException: com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector not found by com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider [129] note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.

为什么会出错?我认为我拥有我需要的一切。我有一个 ivy 文件并尝试添加各种 jackson deps,但似乎没有任何效果。当我将 jackson 添加到我的 ivy 文件时,我无法判断我是否遇到版本问题,因为它包含在 Glassfish 中,或者什么。

Glassfish 提供以下内容:

/d/glassfish4/glassfish/modules/jackson-annotations.jar
/d/glassfish4/glassfish/modules/jackson-core.jar
/d/glassfish4/glassfish/modules/jackson-databind.jar
/d/glassfish4/glassfish/modules/jackson-jaxrs-base.jar
/d/glassfish4/glassfish/modules/jackson-jaxrs-json-provider.jar
/d/glassfish4/glassfish/modules/jersey-media-json-jackson.jar

最佳答案

解决方法如下:

  1. 停止 Glassfish

  2. 删除 Glassfish 模块目录中的所有 jackson 内容。

  3. 删除domains/domain1/osgi-cache/felix目录

  4. 将以下文件复制到该目录:

/glassfish/modules/jackson-annotations-2.4.0.jar

/glassfish/modules/jackson-annotations-2.5.0.jar

/glassfish/modules/jackson-core-2.4.2.jar

/glassfish/modules/jackson-core-2.5.4.jar

/glassfish/modules/jackson-databind-2.4.2.jar

/glassfish/modules/jackson-databind-2.5.4.jar

/glassfish/modules/jackson-jaxrs-base-2.5.4.jar

/glassfish/modules/jackson-jaxrs-json-provider-2.5.4.jar

/glassfish/modules/jackson-module-jaxb-annotations-2.4.2.jar

/glassfish/modules/jackson-module-jaxb-annotations-2.5.4.jar

/glassfish/modules/jersey-media-json-jackson.jar

然后就可以了。不确定 Jackson 的原始文件是什么版本,但这可以工作并升级您的 jackson 模块版本。

关于java - Glassfish 4 REST XML 工作 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31275480/

有关java - Glassfish 4 REST XML 工作 JSON 错误的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  4. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  5. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  6. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  7. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  10. ruby-on-rails - 如何使用 Rack 接收 JSON 对象 - 2

    我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":

随机推荐