Ajax trong Spring MVC
Hiện mình đang làm Ajax trong Spring MVC, khhi controller trả về String thì không có vấn đề gì hết, tuy nhiên nếu trả ra 1 list đối tượng là báo lỗi 406 Not Acceptable. Mình đã tìm hiểu qua Stack overflow nhưng cũng chưa có giải phải nào để xử lý cái này:
Entitiy:
`public class Hocsinh {
private int mahs;
private String ten;
private int tuoi;
public int getMahs() {
return mahs;
}
public void setMahs(int mahs) {
this.mahs = mahs;
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public int getTuoi() {
return tuoi;
}
public void setTuoi(int tuoi) {
this.tuoi = tuoi;
}
public Hocsinh(int mahs, String ten, int tuoi) {
super();
this.mahs = mahs;
this.ten = ten;
this.tuoi = tuoi;
}
public Hocsinh() {
super();
// TODO Auto-generated constructor stub
}
}`
Controller:
`@Controller
public class AjaxController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String home(){
return "index";
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String hello(){
return "Hello Ajax";
}
@RequestMapping(value = "/helloname/{name}", method = RequestMethod.GET)
@ResponseBody
public String helloname(@PathVariable String name){
return "Hello " + name;
}
@RequestMapping(value = "/dshs", method = RequestMethod.GET)
@ResponseBody
public List<Hocsinh> getds(){
HocsinhModel hsm = new HocsinhModel();
return hsm.findAll();
}
}`
Ajax:
$('#btn3').click(function() { $.ajax({ type: "GET", headers: { Accept : "application/json; charset=utf-8", "Content-Type": "application/json; charset=utf-8" }, url: '${pageContext.request.contextPath}/dshs.html', success: function(result){ $('#result2').html("Xong") }, error:function(data,status,er) { alert("error: "+data.tenhang+" status: "+status+" er:"+er); } }); });
pom.xml
`
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>`
Ai có cách nào hướng dẫn mình xử lý với.
Dùng responseEntity ấy:
ref: http://stackoverflow.com/questions/26320106/spring-return-responsebody-responseentitylistjsonobject
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html
Vẫn thế bạn à.
@RequestMapping(value = "/dshs", method = RequestMethod.GET) @ResponseBody public ResponseEntity<Object> getds(){ List<JSONObject> entities = new ArrayList<JSONObject>(); try { HocsinhModel hsm = new HocsinhModel(); for (Hocsinh hs: hsm.findAll()){ JSONObject obj = new JSONObject(); obj.put("mahs", hs.getMahs()); obj.put("ten", hs.getTen()); obj.put("tuoi", hs.getTuoi()); entities.add(obj); } } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } return new ResponseEntity<Object>(entities, HttpStatus.OK); }
Cái produces đâu? trong cái link đưa co mà
Mình đã bổ sung, nhưng chạy vẫn lỗi 406:
`
Cái này do dùng Spring bản 3.2.2, hạ xuống dùng 3.1.4 là hết, đã fix xong lỗi.
bản mới cũng được, trong dispathcher-servlet bạn cần thêm định nghĩa bean
json=application/json xml=application/xml là okCám ơn bạn.