XML Request
/**
* GET - get member
* content type : xml
*
* @param memberId
* @return
*/
@RequestMapping(value = "/{memberId}", method = RequestMethod.GET, headers = "Accept=text/xml")
public Member findOneXml(@PathVariable("memberId") String memberId) {
return memberService.findOne(memberId);
}
Controller
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "member")
@XmlType(name = "", propOrder = {"id", "email", "name", "imagePath"})
public class Member {
public Member(){}
public Member(String id, String email, String name){
this.id = id;
this.email = email;
this.name = name;
}
@NotNull()
@Size(min=2, max=30, message = "INVALID-PARAM-NAME")
@Getter
@Setter
private String id;
//@Pattern(regexp = "/^[0-9a-zA-Z]([\\-.\\w]*[0-9a-zA-Z\\-_+])*@([0-9a-zA-Z][\\-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9}$/")
@Getter
@Setter
private String email;
@Size(min=2, max=30, message = "INVALID-PARAM-NAME")
@Getter
@Setter
private String name;
@Getter
@Setter
private String imagePath;
}
Model
private static HashMap<String, Member> members = new HashMap<String, Member>() {
{
put("attdro", new Member("attdro", "rosd0000@gmail.com", "sam"));
}
};
public Member findOne(String memberId) {
Member m = members.get(memberId);
if (isNull(m)) return null;
m.setImagePath(null);
return m;
}
Data
Select
Service
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<member>
<id>attdro</id>
<email>rosd0000@gmail.com</email>
<name>sam</name>
</member>
Response Data
JSON Request
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
pom.xml - dependency 추가
/**
* GET - get member
* content type : json
*
* @param memberId
* @return
*/
@RequestMapping(value = "/{memberId}", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public Member findOneJson(@PathVariable("memberId") String memberId) {
return memberService.findOne(memberId);
}
Controller
@JsonInclude(JsonInclude.Include.NON_NULL)
Model - 어노테이션 추가
private static HashMap<String, Member> members = new HashMap<String, Member>() {
{
put("attdro", new Member("attdro", "rosd0000@gmail.com", "sam"));
}
};
public Member findOne(String memberId) {
Member m = members.get(memberId);
if (isNull(m)) return null;
m.setImagePath(null);
return m;
}
Data
Select
Service - xml response와 동일
{
"id": "attdro",
"email": "rosd0000@gmail.com",
"name": "sam"
}
JSON Response