Spring MVC - Ví dụ về tạo JSON

Ví dụ sau đây cho thấy cách tạo JSON bằng Spring Web MVC Framework. Để bắt đầu, hãy để chúng tôi có một IDE Eclipse đang hoạt động và xem xét các bước sau để phát triển một Ứng dụng Web dựa trên Biểu mẫu Động bằng cách sử dụng Spring Web Framework -

Bươc Sự miêu tả
1 Tạo một dự án với tên TestWeb trong một gói com.tutorialspoint như đã giải thích trong chương Spring MVC - Hello World.
2 Tạo một lớp Java Người dùng , UserController trong gói com.tutorialspoint .
3 Tải xuống thư viện Jackson Jackson Core, Jackson Databind và Jackson Annotations từ trang kho lưu trữ maven. Đặt chúng vào CLASSPATH của bạn.
4 Bước cuối cùng là tạo nội dung của tất cả các tệp nguồn và cấu hình và xuất ứng dụng như được giải thích bên dưới.

User.java

package com.tutorialspoint;

public class User {
   private String name;
   private int id;
   public String getName() {
      return name;
   }  
   public void setName(String name) {
      this.name = name;
   }
   public int getId() {
      return id;
   }   
   public void setId(int id) {
      this.id = id;
   }	
}

UserController.java

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
	
   @RequestMapping(value="{name}", method = RequestMethod.GET)
   public @ResponseBody User getUser(@PathVariable String name) {

      User user = new User();

      user.setName(name);
      user.setId(1);
      return user;
   }
}

TestWeb-servlet.xml

<beans xmlns = http://www.springframework.org/schema/beans"
   xmlns:context = http://www.springframework.org/schema/context"   
   xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc = http://www.springframework.org/schema/mvc"
   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 = com.tutorialspoint" />
   <mvc:annotation-driven />
</beans>

Ở đây, chúng tôi đã tạo Người dùng POJO đơn giản và trong UserController, chúng tôi đã trả lại Người dùng. Spring tự động xử lý chuyển đổi JSON dựa trên RequestMapping và Jackson jar có trong classpath.

Khi bạn đã hoàn tất việc tạo tệp nguồn và cấu hình, hãy xuất ứng dụng của bạn. Nhấp chuột phải vào ứng dụng của bạn, sử dụngExport → WAR File tùy chọn và lưu của bạn TestWeb.war tệp trong thư mục ứng dụng web của Tomcat.

Bây giờ, khởi động máy chủ Tomcat và đảm bảo rằng bạn có thể truy cập các trang web khác từ thư mục ứng dụng web bằng trình duyệt chuẩn. Hãy thử một URL -http://localhost:8080/TestWeb/mahesh và chúng ta sẽ thấy màn hình sau.