【问题标题】:Jersey JSON not working for latest Jersey APIJersey JSON 不适用于最新的 Jersey API
【发布时间】:2016-11-16 09:46:18
【问题描述】:

我已经通过几个链接了解如何在使用 Jersey 创建的 RESTful Web 服务中返回 JSON 响应。这是我的代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ScraperWebProject</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.myscraper</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>

MyScraper.java

package com.myscraper;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/hello")
public class MyScraper {

    @GET
    @Produces( {MediaType.APPLICATION_JSON })
    public SearchModel sayJsonHello() {
        System.out.println("JSON text hello called");
        SearchModel s = new SearchModel("My First Search", "My first search is done");
        return s;
    }

    @GET
    @Produces( { MediaType.TEXT_XML })
    public SearchModel sayPlainTextHello() {
        System.out.println("Plain text hello called");
        SearchModel s = new SearchModel("My First Search", "My first search is done");
        return s;
    }
}

SearchModel.java

package com.myscraper;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class SearchModel {

    private String title;
    private String description;

    public SearchModel(){

    }
    public SearchModel(String title,String desc){
        this.title = title;
        this.description= desc;

    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "SearchModel [title=" + title + ", desc=" + description + "]";
    }
}

我无法将请求映射到 getJSONHello() 方法。在 Link 找到的解决方案说使用 Jersey JSON,但提供的 xml 对于最新的 Jersey 版本无效。它使用 com.sun 关系属性,该属性仅对旧版本的 Jersey 有效。

【问题讨论】:

标签: java json rest jersey


【解决方案1】:

您需要为 Jersey 2 添加以下依赖项

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

【讨论】:

    【解决方案2】:
    <pre>call api by link     /rest/hello/sayJsonHello  etc. By  Get method because it needs complete path for method sayJsonHello</pre>
    
    
    
    
    package com.myscraper;
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.MediaType;
    
    
        @Path("/hello")
        public class MyScraper {
    
            @GET
        @Path("/sayJsonHello")
            @Produces( {MediaType.APPLICATION_JSON })
            public SearchModel sayJsonHello() {
                System.out.println("JSON text hello called");
                SearchModel s = new SearchModel("My First Search", "My first search is done");
                return s;
            }
    
            @GET
        @Path("/sayPlainTextHello")
            @Produces( { MediaType.TEXT_XML })
            public SearchModel sayPlainTextHello() {
                System.out.println("Plain text hello called");
                SearchModel s = new SearchModel("My First Search", "My first search is done");
                return s;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2017-01-26
      • 1970-01-01
      • 2017-03-21
      • 2012-09-27
      相关资源
      最近更新 更多