Home > Java > javaTutorial > body text

How to interact with json when the return value of the controller method is a simple type?

Y2J
Release: 2017-05-16 09:23:28
Original
1649 people have browsed it

This article mainly introduces the json data interaction controller method of springmvc. The return value is a simple type, which is very practical. Friends in need can refer to it

When the return value of the controller method is a simple type such as String How to interact with json?

Use @RequestBody

For example, the code is as follows:

@RequestMapping(value="/ceshijson",produces="application/json;charset=UTF-8")
@ResponseBody
public String ceshijson(@RequestBody String channelId) throws IOException{

 return channelId;
Copy after login

If the code is the above situation, the front desk sends json When using @RequestBody, it should be written like this (there are many ways to write it, just use it)

function channel(){
   //先获取选中的值
   var channelId = $("#channelId option:selected").val();
   //来判断发送的链接
   if(channelId ==2){


   $.ajax({
     url:"ceshijson",
     type:"post",
     dataType:'json',
     contentType:'application/json;charset=utf-8',
     data:JSON.stringify({'channelId':channelId}),
     success:function(data){
      alert(data.channelId);
     },
     error:function(XMLHttpRequest, textStatus, errorThrown){ 
     alert("Error") 
     alert(XMLHttpRequest.status); 
     alert(XMLHttpRequest.readyState); 
     alert(textStatus); 
     }
   });
   }
  }
Copy after login

Special attention needs to be paid here: As emphasized in the previous article, when @RequestBody is used, it requires String channelId to receive data as json String . That is, if data is written like this: data:{'channelId':channelId}, it is wrong. Because this is jsonobject form.

If you don’t want to use the JSON.stringify() function, then manually splice the strings yourself:

data:'{"channelId":'+channelId+'}'
Copy after login

Also note that channelId is in double quotes and cannot be written as Single quotes because this is a json syntax rule. If you change it to single quotes, that is,

**wrong way of writing

data:"{'channelId':"+channelId+"}"
Copy after login

, although it can be passed to the background, the data returned from the background will be undefined. That is, key must be surrounded by double quotes.

Do not use @RequestBody

 @RequestMapping(value="/ceshijson",produces="application/json;charset=UTF-8")
 @ResponseBody
 public String ceshijson(String channelId) throws IOException{
  Map<String,Object> map = new HashMap<String,Object>();
   map.put("channelId", channelId);
   ObjectMapper mapper = new ObjectMapper();
   channelId = mapper.writeValueAsString(map);
  return channelId;
 }
Copy after login

Front-end code

$.ajax({
   url:"ceshijson",
   type:"post",
   dataType:&#39;json&#39;,
   //contentType:&#39;application/json;charset=utf-8&#39;,
   data:"channelId="+channelId,
   success:function(data){
    alert(data);
   },
   error:function(XMLHttpRequest, textStatus, errorThrown){ 
     alert("Error") 
     alert(XMLHttpRequest.status); 
     alert(XMLHttpRequest.readyState); 
     alert(textStatus); 
    }
});
Copy after login

This method uses writeValueAsString in ObjectMapper to convert Java objects into json strings.

Summary: This method actually does not have much practical meaning, because data is generally not received in this way. Just understand!

【Related Recommendations】

1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download

2. Java free video tutorial

3. JAVA beginner video tutorial

The above is the detailed content of How to interact with json when the return value of the controller method is a simple type?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!