简体   繁体   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;

    }

it always return jsp page rather than returning json object. 它总是返回jsp页面,而不是返回json对象。 so when i hit url in postman it shows me jsp page 因此,当我在邮递员中点击url时,它会向我显示jsp页面

Put @ResponseBody annotation on top of the method like below and change return type to string. @ResponseBody批注放在如下所示方法的顶部,然后将返回类型更改为字符串。

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

and then convert JSON object to string and send like below. 然后将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;

    }

As you are sending jsonobject jackson might not be able to convert it ,you should send string instead 由于您正在发送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