来自Spring REST控制器的JSON响应

时间:2016-08-31 04:38:36

标签: spring rest spring-mvc spring-rest

我收到HTTP错误代码406,其中包含以下错误说明:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

我的目标是在json响应中获取我的POJO类。请找到以下配置:

@RequestMapping(value="/testjson",produces="application/json")
    public @ResponseBody Employee testjson () {
        System.err.println("testing json");
        Employee testEmp = new Employee("1", "Ankit", "Agarwal");

        return testEmp;
    }

弹簧servlet.xml中

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
        <map>
           <entry key="json" value="application/json" />
           <entry key="xml" value="application/xml" />
           <entry key="rss" value="application/rss+xml" />
        </map>
      </property>
    </bean>

1 个答案:

答案 0 :(得分:0)

试试这个

<RelativeLayout
android:id="@+id/rlTrainRoot"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:id="@+id/llArrivalTime"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:orientation="vertical"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="15dp"
    android:layout_marginEnd="15dp"
    android:background="@color/ArrivalTimeGreen"
    android:onClick="ArrivalTimeOnClickListener">