找不到媒体类型= application / json的MessageBodyWriter [英] MessageBodyWriter not found for media type=application/json

查看:119
本文介绍了找不到媒体类型= application / json的MessageBodyWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JSON作为JAX-RS服务时遇到问题。

I am facing issues while consuming JAX-RS services as JSON.

下面我添加了我的代码。

Below I have added my code.

这是我的服务类:

//Sets the path to base URL + /hello
@Path("/hello") 
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Hello {

    @GET
    @Produces("application/json")
    public Student getStudent() {

        Student s = new Student();
        s.first_name = "Test First Name !!!";
        s.last_name = "Test Last Name!!!";

        return s;
    }

学生类哪个我想从服务中获得:

Student class which I am trying to get from service:

@XmlRootElement
public class Student implements Serializable {

    public String first_name;
    public String last_name;

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public Student()
    {
        first_name = "Fahad";
        last_name = "Mullaji";
    }
}

服务端的Web XML。

Web XML on service side.

<?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>com.vogella.jersey.first</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>com.vogella.jersey.first</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>/rest/*</url-pattern>
    </servlet-mapping>
</web-app> 

我不知道如何解决这个问题。
我正在使用SOAP UI来测试JSON响应,但我想它应该没关系。

I don't know how to fix this issue. I am using SOAP UI for testing JSON response but I guess that it should not matter.

我读过很多地方我需要添加下面的代码。但我不知道在哪里。我没有使用Maven进行构建。

Many places I read that I need to add the code below. But I don't know where. I am not using Maven for building.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
</dependency>

服务器正在寻找解析学生的功能对象为JSON,但它无法找到它的函数或jar文件。
我添加了Genson,Moxy,RESTEasy和Jackson的jar,但我认为这是问题所在。
我想我在某个地方缺少映射。

Server is looking for function to parse Student object to JSON but it is not able to find function or jar file for it. I have added jar of Genson, Moxy, RESTEasy and Jackson but I think that's the problem. I think I am missing mapping somewhere.

推荐答案

好吧,我无法使上述Web项目正常工作。但我继续研究JAX-RS,我遇到了Stormpath的教程。我想无论谁想要学习REST,JSON和Jersey如何协同工作都应该注意这一点。

Well, I could not make the above web project working. But I kept on doing research on JAX-RS and I came across Stormpath's tutorial. I think whoever is trying to learn how REST, JSON and Jersey work together should watch this.

请参阅下面的链接,了解他们在GitHub上的演示和示例REST项目。

Please see links below for their presentation and sample REST project on GitHub .

https://www.youtube.com/观看?v = sVvL12BnIyQ

https ://github.com/stormpath/todos-jersey

现在我可以比较上面的项目,看看我做错了什么。

Now I can compare above project and see what all things I did wrong.

这篇关于找不到媒体类型= application / json的MessageBodyWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆