Java 如何在Spring中发送JSON响应?

Java 如何在Spring中发送JSON响应?,java,json,spring,Java,Json,Spring,我想在spring应用程序中从服务器返回JSON响应。 下面是我的代码片段 @RequestMapping(value="getCustomer.action", method = RequestMethod.GET) public @ResponseBody Customer getValidCustomer(Model model) { System.out.println("comes"); Customer

我想在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;

    }
但是我在客户端收到一个错误。

您需要:

  • 添加到类路径
  • 添加到配置中
  • 返回
    Map

阅读:

下面给出了示例*-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格式的响应来发送请求


关于弹簧休息的详细解释可以在

上找到,因为你已经有了一个答案,里面有一些细节,我想我会举一个例子。给你:

    @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响应。不需要其他配置

//我创建了一个类,用于将简单字符串转换为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()方法中的数据

/jspjQuery代码

               var app=jQuery.parseJSON(response);

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

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

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

您有什么错误?请检查是否包含Jackson库
               var app=jQuery.parseJSON(response);

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

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

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