如何在Spring MVC中的Rest Controller中将Bean类对象作为JSON返回

时间:2018-06-29 06:18:26

标签: spring-mvc spring-boot

这里的控制器就像

 @RequestMapping(value = "/restCallRequest", headers = 
 "Accept=application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Void> callRequest(@RequestBody CallRequestData  
 requestData, UriComponentsBuilder ucBuilder) {

    if (requestData.getIvrName().isEmpty()) {
        return new ResponseEntity<Void>(HttpStatus.CONFLICT);
    }
    System.err.println("IVER Name is "+requestData.getIvrName()+" lsit "+requestData.getContactList().get(0));

   HttpHeaders headers = new HttpHeaders();
   headers.setLocation(ucBuilder.path("/restCallRequest").buildAndExpand(requestData).toUri());
  return new ResponseEntity<Void>(headers, HttpStatus.CREATED); 
}

我的Bean类是

@Entity
public class CallRequestData {
private int id;   //Auto incremented
private String ivrName;
private List<String> contactList;
public CallRequestData(int id, String ivrName, List<String> contactList) {
    this.id = id;
    this.ivrName = ivrName;
    this.contactList = contactList;
}
//setters And Getters

这是我的POSTMAN发送JSON

{
    "ivr_name":"welcome",
    "contactList":[
           "9040210495",
           "958045830"
    ]
}

我也希望像Request这样的Response,     {         “ ivr_name”:“欢迎”,         “联系人列表”:[                “ 9040210495”,                “ 958045830”         ]     }

  

我该如何解决。提前谢谢。

3 个答案:

答案 0 :(得分:0)

在pom.xml中添加依赖项

<dependency>
    <groupId>net.sf.flexjson</groupId>
    <artifactId>flexjson</artifactId>
    <version>2.1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>

在控制器中编写一种方法。

public String toJson(User bean) { 
  return new JSONSerializer().transform(new DateTransformer("MM/dd/yyyy HH:mm:ss"),java.util.Date.class).serialize(bean);
    }

并将您的bean对象传递给此方法。

return new ResponseEntity<String>(toJson(bean),headers, HttpStatus.CREATED); 

在方法签名public ResponseEntity<String> callRequest中将返回类型更改为字符串

注意:对于Spring引导,您可以使用@RestController批注,它将自动将对象转换为json。

答案 1 :(得分:0)

添加Jackson依赖项

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.4</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.4</version>
  <scope>provided</scope>
</dependency> 

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.4</version>
  <scope>provided</scope>
</dependency>

并添加

@RequestMapping(value="/endpoint",produces="application/json",method=RequestMethod.POST)

produces="application/json"到请求的映射。 \

无论如何,在@ResponseBody之后定义的bean将作为json对象返回给客户端。无需在外部将其解析为json。

答案 2 :(得分:0)

我不明白您为什么选择所有这些选项。 很简单。

如果您的控制器带有@RestController注释,则只需返回以下对象:

@RequestMapping..................................)
 public ResponseEntity<CallRequestData> callRequest(@RequestBody CallRequestData 
 requestData, UriComponentsBuilder ucBuilder) {

.........这里的代码......

  return new ResponseEntity<>(requestData, HttpStatus.CREATED); 

}

在响应实体上设置返回类型,然后返回,只需返回一个新的ResponseEntity实例即可。

我建议您避免多次RETURN,将变量分配给ResponseEntity并在方法末尾仅返回一次