使用httprequester接口测试能返回数据,但是用ajax返回json格式的时候返回报500Internal Server Error。
The server encountered an internal error that prevented it from fulfilling this request org.springframework .web.util.NestedServletException: Request processing failed; nested exception is…
springMVC+ajax 400 bad request —-请求的参数变量名-类型与后台的同名参数类型不匹配导致的,比如sys_no后台是String,前台页面传输的是数字类型就会出现这样的错误,springmvc直接将这个错误封装成400抛给了前端。开始提交是contentType : “application/json”形式,就报了400的错误,后面改成表单提交方式。
ajax提交参数到springmvc的后台,一直获取不到参数值,参数值为null——请求的方式不对,导致参数没有按后台约定的形式传递。比如下面实例的Controller接收的是一个名值对param是参数名、param的参数值是字符串类型。开始前台页面提交的数据是:data:param,后台获取param是得不到参数值的,因为到后台其实封装成了4个类似这样的参数名值对:sys_no=sys_no+”“,log_content=log_content+”“,page_size=15,page_no=page_no,并没有名称为param的参数,所以最后继续封装,将前端的param对象转成字符串,参数名为param,提交给后台:data : {“param”:JSON.stringify(param)},OK!
(本文章分享在CSDN平台,更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217)
使用注解@ResponseBody可以将结果(一个包含字符串和JavaBean的Map),转换成JSON。 使用 @RequestBody 注解前台只需要向 Controller 提交一段符合格式的 JSON,Spring 会自动将其拼装成 bean。 Spring这个转换是靠org.codehaus.jackson这个组件来实现的,所有需要引入jackson-core-asl和org.codehaus.jackson两个jar包 :
pom.xml:
<dependency>
<groupId>org.codehaus.jacksongroupId>
<artifactId>jackson-core-aslartifactId>
<version>1.9.13version>
dependency>
<dependency>
<groupId>org.codehaus.jacksongroupId>
<artifactId>jackson-mapper-aslartifactId>
<version>1.9.13version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.6version>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20140107version>
dependency>
<dependency>
<groupId>net.sf.json-libgroupId>
<artifactId>json-libartifactId>
<version>2.4version>
<classifier>jdk15classifier>
dependency>
spring-mvc.xml文件配置:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
bean>
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;charset=UTF-8" />
<property name="features">
<array>
<value>WriteMapNullValuevalue>
<value>WriteNullStringAsEmptyvalue>
array>
property>
bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
bean>
property>
bean>
property>
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8value>
<value>text/html;charset=utf-8value>
<value>application/json;charset=UTF-8value>
<value>application/jsonvalue>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
Controller:
package org.byron4j.ynyn.controller;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.byron4j.ynyn.base.BaseLoggerController;
import org.byron4j.ynyn.dto.response.QueryLoggerInfoResDto;
import org.byron4j.ynyn.service.QueryLoggerService;
import org.byron4j.ynyn.service.TaskListenerConfigService;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@RestController
public class QueryLoggerInfoController extends BaseLoggerController{
@Autowired
QueryLoggerService queryLoggerService;
@ResponseBody
@RequestMapping(value="/queryLoggerInfo",method=RequestMethod.POST)
public Object queryLoggerInfo(HttpSession session, String param) throws Exception{
QueryLoggerInfoResDto obj = queryLoggerService.queryLoggerInfo(param);
// 测试返回对象的Jackson json转换
ObjectMapper om = new ObjectMapper();
try {
om.writeValueAsString(obj);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return obj ;
}
}
响应实体对象:
package org.byron4j.ynyn.dto.response;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.byron4j.ynyn.base.BaseResDto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
/**
*
* @Getter @Setter 是lombok的注解--小而巧的代码生成插件--减少实体类代码量
* 实例详解可参考:http://blog.csdn.net/zixiao217/article/category/6302134
*/
@Getter
@Setter
@JsonInclude(Include.ALWAYS)
public class QueryLoggerInfoResDto extends BaseResDto{
private static final long serialVersionUID = -6778903837998066869L;
private int total_amount;
List log_list;
}
响应实体类的基类:
package org.byron4j.ynyn.base;
import java.io.Serializable;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Data
@JsonInclude(Include.ALWAYS)
public class BaseResDto implements Serializable{
private static final long serialVersionUID = 6926791530160100430L;
private String resultCode;
private String resultDesc;
}
使用jquery的ajax的jsp代码:
var param = {
"sys_no":sys_no+"",
"log_content":log_content+"",
"page_size":15,
"page_no":page_no
}
$.ajax({
type : "POST",
async:false,
url : window.location+"queryLoggerInfo",
/*使用表单提交方式*/
contentType : "application/x-www-form-urlencoded",
dataType:"json",
/*后台接收的是参数名为param(Controller方法的参数可以看出),所以需要封装成param:值的形式(字符串)*/
data : {"param":JSON.stringify(param)},
success : function(data) {
...
}
});