我返回json对象并得到html的响应

时间:2018-12-20 10:28:03

标签: java spring-mvc

@RequestMapping(value="/list" ,method=RequestMethod.GET,produces = "application/json",headers = "Accept=application/json")

    public JSONObject  list(Model model,HttpServletResponse response,ModelAndView mv) {
        response.setContentType("application/json");
        List<TblTours>tbl=tblToursService.getAll();
        JSONObject jSONObject=new JSONObject();
          JSONArray jsArray = new JSONArray(tbl);
          jSONObject.put("data",jsArray );
          System.out.println("jsArray"+jsArray);

        return jSONObject;

    }

它总是返回jsp页面,而不是返回json对象。因此,当我在邮递员中点击url时,它将向我显示jsp页面

3 个答案:

答案 0 :(得分:0)

@ResponseBody注释放在下面的方法顶部,然后将返回类型更改为字符串。

@RequestMapping(value="/list" ,method=RequestMethod.GET,produces = "application/json",headers = "Accept=application/json")
@ResponseBody
public String list(Model model,HttpServletResponse response,ModelAndView mv) 

,然后将JSON对象转换为字符串并按照以下方式发送。

return jSONObject.toString();

答案 1 :(得分:0)

@RequestMapping(value="/list" ,method=RequestMethod.GET,produces = "application/json",headers = "Accept=application/json")
@ResponseBody

    public JSONObject  list(Model model,HttpServletResponse response,ModelAndView mv) {
        response.setContentType("application/json");
        List<TblTours>tbl=tblToursService.getAll();
        JSONObject jSONObject=new JSONObject();
          JSONArray jsArray = new JSONArray(tbl);
          jSONObject.put("data",jsArray );
          System.out.println("jsArray"+jsArray);

        return jSONObject;

    }

由于您正在发送jsonobject jackson,可能无法将其转换,因此您应该发送字符串

@RequestMapping(value="/list" ,method=RequestMethod.GET,produces = "application/json",headers = "Accept=application/json")
@ResponseBody

    public String  list(Model model,HttpServletResponse response,ModelAndView mv) {
        response.setContentType("application/json");
        List<TblTours>tbl=tblToursService.getAll();
        JSONObject jSONObject=new JSONObject();
          JSONArray jsArray = new JSONArray(tbl);
          jSONObject.put("data",jsArray );
          System.out.println("jsArray"+jsArray);

        return jSONObject.toString();

    }

答案 2 :(得分:-2)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.hib.controller" />
    <context:component-scan base-package="com.hib.model" />
    <context:component-scan base-package="com.hib.daoImplementation" />

    <context:component-scan base-package="com.hib.userserviceImplementation" />

    <context:component-scan base-package="com.app.repository" />

    <mvc:annotation-driven />

    <bean id="dataSource111" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/hibspring" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource111" />

        <property name="annotatedClasses">
            <list>
                <value>com.hib.model.User</value>

            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven/> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
    </beans>