springMvc如何接受多個對象參數


在代碼開發過程中,參數的定義非常重要,目前springmvc提供的較多的參數獲取方式,譬如  @PathVariable 和 @RequestParam,

或者通過增加如下注解

<mvc:annotation-driven>
    </mvc:annotation-driven>
mvc:annotation-driven默認增加了MappingJackson2HttpMessageConverter,可以將傳遞進來的body體解析成對應的JAVA對象。

但是存入如下問題
1、@PathVariable 和 @RequestParam只能解析基礎的Java格式,int string boolean等,
2、MappingJackson2HttpMessageConverter只能將對象轉變為一個JAVA Bean,
如果我們有兩個java bean 定義如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public  class  Student {
     private  String name;
     private  int  year;
     public  String getName() {
         return  name;
     }
     public  void  setName(String name) {
         this .name = name;
     }
     public  int  getYear() {
         return  year;
     }
     public  void  setYear( int  year) {
         this .year = year;
     }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public  class  Info {
     private  String city;
     private  String address;
 
     public  String getCity() {
         return  city;
     }
 
     public  void  setCity(String city) {
         this .city = city;
     }
 
     public  String getAddress() {
         return  address;
     }
 
     public  void  setAddress(String address) {
         this .address = address;
     }
}

  

如果我們定義了一個接口,需要傳遞這兩個對象信息,采用MappingJackson2HttpMessageConverter就會比較麻煩。

這里講解一個如何實現同時傳遞多個對象的方法,我們最終定義的接口如下

public Student addstudentpost(@JsonObject Student student,@JsonObject Info info)

原理是實現我們自定義的HandlerMethodArgumentResolver,並且插入到springMvc的參數解析隊列中。
1、首先定義一個注解類
1
2
3
4
5
@Target (ElementType.PARAMETER)
@Retention (RetentionPolicy.RUNTIME)
@Documented
public  @interface  JsonObject {
}

 2、其次實現自定義的參數解析類JsonObjectArgResolverHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public  class  JsonObjectArgResolverHandler  implements  HandlerMethodArgumentResolver {
 
     @Override  public  boolean  supportsParameter(MethodParameter methodParameter) {
         return  methodParameter.hasParameterAnnotation(JsonObject. class );
     }
 
     @Override  public  Object resolveArgument(MethodParameter methodParameter,
         ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest,
         WebDataBinderFactory webDataBinderFactory)  throws  Exception {
         try  {
             JSONObject para = getRequestInfo(nativeWebRequest);
             Class<?> type = methodParameter.getParameterType();
             String name = methodParameter.getParameterName();
             if  ( null  != para && para.containsKey(name)) {
                 return  JSON.parseObject(para.getString(name), type);
             }
         catch  (Exception e) {
         }
         return  null ;
     }
 
 
 
     private  JSONObject getRequestInfo(NativeWebRequest webRequest)  throws  IOException {
         JSONObject para =  new  JSONObject();
         HttpServletRequest httpServletRequest =
             (HttpServletRequest) webRequest.getNativeRequest(HttpServletRequest. class );
         String method = httpServletRequest.getMethod();
         if  (!method.equals( "GET" ) && !method.equals( "DELETE" )) {
 
             if  ( null  != httpServletRequest.getAttribute( "para" )) {
                 try  {
                     para = JSON.parseObject(httpServletRequest.getAttribute( "para" ).toString());
                 catch  (Exception e) {
                 }
             else  {
                 StringBuilder buffer =  new  StringBuilder();
                 BufferedReader reader = httpServletRequest.getReader();
                 String line;
                 while  ((line = reader.readLine()) !=  null ) {
                     buffer.append(line);
                 }
                 httpServletRequest.setAttribute( "para" , buffer.toString());
 
                 try  {
                     para = JSON.parseObject(buffer.toString());
                 catch  (Exception e) {
                 }
             }
         else  {
             Map<String, String[]> parameterMap = webRequest.getParameterMap();
             for  (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                 String key = entry.getKey();
                 String values = StringUtils.join(entry.getValue());
                 para.put(key, values);
             }
         }
         return  para;
     }
 
}

  注意:

a、supportsParameter表明我們的類只支持解析帶有JsonObject的對象解析。

b、httpServletRequest的body體只能讀取一次,再次讀取后就返回空,因為帶有JsonObject注解的對象都會執行一遍,在第一次獲取后需要將body體保存下來,以便下次使用。所有有如下的代碼

     將body體保存

1
httpServletRequest.setAttribute( "para" , buffer.toString());

 讀取Attribute,沒有則從body體讀取。

1 if (null != httpServletRequest.getAttribute("para")) {
2                 try {
3                     para = JSON.parseObject(httpServletRequest.getAttribute("para").toString());
4                 } catch (Exception e) {
5                 }
6             } else {

3、將 JsonObjectArgResolverHandler配置進xml文件中。

復制代碼
<mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="com.nuaa.handler.JsonObjectArgResolverHandler"></bean>
        </mvc:argument-resolvers>
        <mvc:return-value-handlers>
            <bean class="com.nuaa.handler.ReponseJsonBodyMethodReturnValueHandler">
                <property name="messageConverters">
                    <list>
                        <bean class="com.nuaa.handler.Base64JsonHttpMessageConverter"/>
                    </list>
                </property>
            </bean>
        </mvc:return-value-handlers>
    </mvc:annotation-driven>
復制代碼

 

 

 

 

這個我們可以定義如下的接口,

 

@RequestMapping(value="/addstudentpost", method = RequestMethod.POST,produces="application/json")
@ResponseJsonBody
public Student addstudentpost(@JsonObject Student student,@JsonObject Info info){
student.setName(JSON.toJSONString(student)+JSON.toJSONString(info)+(new Date()));
return student;
}

整個函數的定義就比較明了和直觀。

 

關於 ResponseJsonBody的注解,請查看http://www.cnblogs.com/wangjiuyong/articles/7162207.html

 

在readme中有測試方法:

復制代碼
http://localhost:8080/spring/controlhandler/addstudentget?student={"name":"zhang","year":100}&info={"address":"yuhuataiqu","city":"nanjing"}


http://localhost:8080/spring/controlhandler/addstudentpost
{
  "student": {
    "name": "wangjiuyong",
    "year": 2000
  },
  "info": {
    "address": "yuhuataiqu",
    "city": "nanjing"
  }
}
復制代碼
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM