springmvcjson数据处理

编程

@ResponseBody / @RequestBody ,可以将响应值转换成json / 将请求参数转换成json,Jackson可对属性进行json格式化处理,引入依赖

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.10.2</version>

</dependency>

  • pojo

/**

* @JsonIgnore:属性忽略

* @JsonIgnore:日期转换

* @JsonInclude:属性排除

*/

@Data

@AllArgsConstructor

public class User {

private Integer id;

@JsonIgnore

private String name;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")

private Date date;

@JsonInclude(JsonInclude.Include.NON_EMPTY)

private List<String> hobby;

}

  • controller

@RestController

public class JsonController {

@RequestMapping("/json/date")

public User test1(){

ArrayList<String> list = new ArrayList<>();

User user = new User(1001, "张三", new Date(),list);

return user; // 响应值:{"id":1001,"date":"2020-08-01 15:48:38"}

}

@RequestMapping(value = "/json/CN",produces = "text/html;charset=utf-8")

public String test2(){

return "如鱼得水"; // 响应值:如鱼得水

}

}

以上是 springmvcjson数据处理 的全部内容, 来源链接: utcz.com/z/519111.html

回到顶部