restful webservice json数据get方法(myeclipse)

创建web service project

添加代码

RestFul.java:

package com.webservice;


import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;





import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




import javax.ws.rs.GET;

import net.sf.json.JSONArray;


import net.sf.json.JSONObject;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value = "/getData")
public class RestFul {
    
    // http://localhost:8080/RestWebService/getData?userName=sun 方式的调用
    @RequestMapping
    public void printData1(HttpServletRequest request, HttpServletResponse response,
    @RequestParam(value="userName", defaultValue="User") String name) {
        List uiList = new ArrayList();
        for (int i=0; i<3; i++)
        {
            UserInfo ui = new UserInfo();
            //JSONObject map = new JSONObject();
            ui.ID = i;
            ui.Name = "SUN" + i;
            ui.Position = "Position" + i;
            uiList.add(ui);
            }
 
        JSONArray jsonArr = JSONArray.fromObject(uiList);
        String msg = jsonArr.toString();
        printData(response, msg);
    }


    // http://localhost:8080/RestWebService/getData/Sun/Royi 方式的调用
    @RequestMapping(value = "/{firstName}/{lastName}")
    public void printData2(HttpServletRequest request, HttpServletResponse response,
        @PathVariable String firstName, @PathVariable String lastName) {
        String msg = "Welcome "+ firstName + " " + lastName;
        printData(response, msg);
    }
    
    // 转换成HTML形式返回
    private void printData(HttpServletResponse response, String msg) {
        try {
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
            out.println(msg);
            out.close();
        } catch (Exception e) {  
            e.printStackTrace();
        }
   }

    // http://localhost:8080/RestWebService/getData/json?item=0 方式的调用
    @RequestMapping(value = "/json")
    public void printData3(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value="item", defaultValue="0") String item) {
        printDataJason(response, item);
    }


    // http://localhost:8080/RestWebService/getData/json/1 方式的调用
    @RequestMapping(value = "/json/{item}")
    public void printData4(HttpServletRequest request, HttpServletResponse response,
        @PathVariable String item) {
        printDataJason(response, item);
    }
    
    // JSON格式化
    private void printDataJason(HttpServletResponse response, String item) {
        String st = "";
        try {
           response.setContentType("text/html;charset=utf-8");
           response.setCharacterEncoding("UTF-8");
           PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
           
           List uiList = new ArrayList();
           for (int i=0; i<3; i++)
           {
               UserInfo ui = new UserInfo();
               //JSONObject map = new JSONObject();
               ui.ID = i;
               ui.Name = "SUN" + i;
               ui.Position = "Position" + i;
               uiList.add(ui);
               out.close();
               }
     
           JSONArray jsonArr = JSONArray.fromObject(uiList);
           st = jsonArr.toString();
           }
           catch (Exception e) {  
               e.printStackTrace();  
           }
        printData(response, st);
        }
 
    // http://localhost:8080/RestWebService/getData/query
    @GET
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    private String printDataJason1() {
        String st = "";
        try {
           List uiList = new ArrayList();
           for (int i=0; i<3; i++)
           {
               UserInfo ui = new UserInfo();
               //JSONObject map = new JSONObject();
               ui.ID = i;
               ui.Name = "SUN" + i;
               ui.Position = "Position" + i;
               uiList.add(ui);
               }
     
           JSONArray jsonArr = JSONArray.fromObject(uiList);
           st = jsonArr.toString();
           }
           catch (Exception e) {  
               e.printStackTrace();  
           }
        return st;
        }
}


添加代码
UserInfo.java

package com.webservice;

public class UserInfo{
    
    Integer ID;
    
    String Name;
    
    String Position;

    public Integer getID() {
        return ID;
    }

    public void setID(Integer iD) {
        ID = iD;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getPosition() {
        return Position;
    }

    public void setPosition(String position) {
        Position = position;
    }
    
}



添加配置文件

web.xml



  RestWebService

    
    
        springMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/springmvc-servlet.xml
        
        2
    

    
        springMVC
        /
    


配置文件rest-servlet.xml



 
    
    
 
   
    
        
            
                
            
        
    
 
    
    
    
 
    
 


配置文件springmvc-servlet.xml

  
  
 
      
      
      
 
      
      
 
      
      
          
      
 
      
      
          
              
                  
              
          
      
 
      
          
              
                text/plain;charset=UTF-8  
              
          
      
 
      
      
 
  


工程目录

restful webservice json数据get方法(myeclipse)_第1张图片

注意事项

导入包时一定要把json相关jar包放到项目的lib目录下

restful webservice json数据get方法(myeclipse)_第2张图片

你可能感兴趣的:(json,restful,webservice)