使用jersey在REST post中使用JSON对象

时间:2013-02-08 09:35:00

标签: rest post jersey

我写了一个REST-POST服务泽西岛。我在请求中传递JSON并将其转换为Java对象。我能够使用Gson Marshall / unmarshall Item对象。 请使用泽西束中的类提供解决方案。 以下是使用Web应用程序的以下jar     Java的json.jar     球衣束,1.17.jar 我无法解决stackoverflow中提供的解决方案。     当我使用java客户端调用REST Post方法时,我收到以下异常:

SEVERE: A message body reader for Java class  com.item.Item, and Java type class  com.item.Item, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader

这是我写过的POST请求的REST服务。

@Path("/Items")
public class ItemResources {

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response insertItem(Item newItem) {                
    Response res = null;                
    System.out.println("Posting new item");
    ItemDAO.getInstance().getItems().put(newItem.getId(), newItem);
    res = Response.created(uriInfo.getAbsolutePath()).build();
    return res;     
  }
}

客户端实现是:

public class ItemClient {
        public static void main(String[] args) {

            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);
            WebResource service = client.resource(getBaseURI());ContactId cid3 = new ContactId("3", "NW-3", "Code-3");
            Contact cn3 = new Contact(cid3, "user-3", "user-3/profile");
            Item item3 = new Item("UID-3", false, "en", "mar-12", cn3);
            Gson gson = new Gson();
            String jsonStr = gson.toJson(item3);

            ClientResponse response = service.path("Items")
                    .header("content-type", MediaType.APPLICATION_JSON)
                    .post(ClientResponse.class, item3);
            System.out.println("Posting response:" + response);


        }

        private static URI getBaseURI() {
            return UriBuilder.fromUri("http://localhost:8080/ItemJersey")
                    .build();
        }
    }

在web.xml中,我在servlet配置中添加了以下内容。

    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name>
          <param-value>com.item.resource</param-value>
        </init-param>
        <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

0 个答案:

没有答案