java 为什么缺少媒体类型应用程序/json 的编写器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3630827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:44:03  来源:igfitidea点击:

Why writer for media type application/json missing

javajsonresteasy

提问by markl

Basically I have a restful service (post) that consumes(application/json) and produces (application/json). The single param for this service is an annotated java object.

基本上我有一个宁静的服务(post),它消耗(application/json)并产生(application/json)。此服务的单个参数是一个带注释的 java 对象。

I am using org.jboss.resteasy.client.ClientRequestto send the request to the service. However, I am getting this exception in the client end and the exception:

我正在使用org.jboss.resteasy.client.ClientRequest将请求发送到服务。但是,我在客户端和异常中收到此异常:

could not find writer for content-type application/jsontype.

找不到内容类型application/json类型的作者。

Does this mean that I am missing some library jars or I have to write my own writer for application/json?

这是否意味着我缺少一些库 jar 或者我必须为 application/json 编写自己的编写器?

I am using resteasy 1.1

我正在使用resteasy 1.1

Mark

标记

回答by Dan

Raman is correct. Jettison is a valid option. You can also use Hymanson. If you are using maven, it is as simple as including the following dependency in you pom:

拉曼是​​对的。抛弃是一个有效的选择。您也可以使用Hyman逊。如果你使用的是 maven,它就像在你的 pom 中包含以下依赖一样简单:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-Hymanson-provider</artifactId>
        <version>2.3.2.Final</version>
    </dependency>

At which point you should have no problem writing code such as:

在这一点上,您编写代码应该没有问题,例如:

    SomeBean query = new SomeBean("args")
    request.body("application/json", query);
    ClientResponse response = request.post();

回答by Ramanqul Buzaubak

actually I had the same problem, I did solve it by adding jettison provider for application/json mime type. I don't know whether resteasy 1.1 containts jettison provider but version 1.2 does. Also if you are using jdk 1.6 you must exclude javax.xml.stream:stax-api jar file, otherwise you will have a problem.

实际上我遇到了同样的问题,我确实通过为 application/json mime 类型添加 jettison 提供程序来解决它。我不知道 resteasy 1.1 是否包含抛弃提供程序,但 1.2 版包含。此外,如果您使用的是 jdk 1.6,则必须排除 javax.xml.stream:stax-api jar 文件,否则会出现问题。

Here is the example:

这是示例:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="account")
public class Account {

    private Long id;
    private String accountNo;


    public Account(){}
    public Account(String no)   {
        accountNo=no;
    }


    @Id
    @XmlElement
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @XmlElement
    public String getAccountNo() {
        return accountNo;
    }
    public void setAccountNo(String a) {
        accountNo = a;
    }

}

and JAXB class:

和 JAXB 类:

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


    @Path("/account")
    public class AccountService {


        @GET
        @Path("/{accountNo}")
        @Produces("application/json")
        public Account getAccount(@PathParam("accountNo") String accountNo) {
                   return new Account(accountNo);
        }

    }

That's all, have a nice day!

就这样,祝你有美好的一天!

回答by fmucar

Add below to the Resource class or the method causing the exception

将下面添加到 Resource 类或导致异常的方法

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)