簡體   English   中英

我返回json對象並得到html的響應

[英]i return json object and getting response of html

@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頁面

@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();
@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();

    }
<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM