如何在春天发送Json响应?

时间:2013-09-13 07:15:22

标签: java json spring

我想使用spring从服务器端返回json响应。 我的代码就是这个。

@RequestMapping(value="getCustomer.action", method = RequestMethod.GET)
    public @ResponseBody Customer getValidCustomer(Model model) {
        System.out.println("comes");
        Customer customer2 = (Customer) customerService
                .getCustomer("vvmnbv@jgfj.ghfjg");
        System.out.println(customer2.getEmail());
        return customer2;

    }

但它在前端发出错误。输入代码

4 个答案:

答案 0 :(得分:2)

你需要:

  • Jackson JSON Mapper添加到类路径
  • <mvc:annotation-driven>添加到您的配置
  • 返回Map<Integer, String>

阅读: http://blog.safaribooksonline.com/2012/03/28/spring-mvc-tip-returning-json-from-a-spring-controller/

答案 1 :(得分:2)

由于你已经对其中的某些细节做出了回答,我想我只会举一个例子。你走了:

    @RequestMapping(value = "/getfees", method = RequestMethod.POST)
public @ResponseBody
DomainFeesResponse getFees(
        @RequestHeader(value = "userName") String userName,
        @RequestHeader(value = "password") String password,
        @RequestHeader(value = "lastSyncDate", defaultValue = "") String syncDate) {

    return domainFeesHelper.executeRetreiveFees(userName, password, syncDate);
}

只是一个小小的总结:如您所知,您将需要类路径中的Jackson库,以便将对象转换为JSON。

@ResponseBody告诉spring转换其返回值并自动将其写入HTTP响应。无需其他配置。

答案 2 :(得分:0)

示例* -servlet.xml配置如下所示。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="org.smarttechies.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
   <property name="mediaTypes">
      <map>
        <entry key="html" value="text/html"></entry>
        <entry key="json" value="application/json"></entry>
        <entry key="xml" value="application/xml"></entry>
      </map>
   </property>
   <property name="viewResolvers">
      <list>
        <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <property name="prefix" value="/WEB-INF/jsp/"/>
           <property name="suffix" value=".jsp"/>
        </bean>
      </list>
   </property>
</bean>
</beans>

然后将应用程序部署到服务器并通过将“Accept”标头设置为“application / json”来发送请求,以获得JSON格式的响应或“application / xml”以获取XML格式的响应。

有关Spring REST的详细帖子可在http://smarttechie.org/2013/08/11/creating-restful-services-using-spring/

获取

答案 3 :(得分:0)

//我创建了一个用于将简单字符串转换为json可转换格式的类 并将其返回到JSP页面,在那里它被解析为json并像

一样使用
  public class Json {    

    public static String Convert(Object a,Object b){
    return " \""+a.toString()+"\" : \""+b.toString()+"\",";
}

  public static String ConvertLast(Object a,Object b){
    return " \""+a.toString()+"\" : \""+b.toString()+"\" }";
}
public static String ConvertFirst(Object a,Object b){
    return "{ \""+a.toString()+"\" : \""+b.toString()+"\",";
}    }

//控制器代码忽略我放入conver(),convertLast()和convertFirst()方法的数据

String json = Json.ConvertFirst("apId", appointment.getId())
                + Json.Convert("appDate",
                        format.format(appointment.getAppointmentdate()))
                + Json.Convert("appStart", formathourse.format(appointment
                        .getAppointmentstarttime()))
                + Json.Convert("appEnd", formathourse.format(appointment
                        .getAppointmentendtime()))
                + Json.Convert("PatientId", appointment.getPatientId()
                        .getId())
                + Json.Convert("PatientName", appointment.getPatientId()
                        .getFname()
                        + " "
                        + appointment.getPatientId().getLname())
                + Json.Convert("Age", appointment.getPatientId().getAge())
                + Json.Convert("Contact", appointment.getPatientId()
                        .getMobile())
                + Json.Convert("Gender", appointment.getPatientId()
                        .getSex())
                + Json.ConvertLast("Country", appointment.getPatientId()
                        .getCountry());
        return json;}

/ JSP JQuery Code

               var app=jQuery.parseJSON(response);

                $("#pid").html(app.PatientId);

                $("#pname").html(app.PatientName);

                $("#pcontact").html(app.Contact);