Skip to content

Commit 632de4c

Browse files
Added UserController and User object
1 parent 575f306 commit 632de4c

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jsondoc.sample.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.jsondoc.core.annotation.Api;
7+
import org.jsondoc.core.annotation.ApiError;
8+
import org.jsondoc.core.annotation.ApiErrors;
9+
import org.jsondoc.core.annotation.ApiMethod;
10+
import org.jsondoc.core.annotation.ApiParam;
11+
import org.jsondoc.core.annotation.ApiResponseObject;
12+
import org.jsondoc.core.pojo.ApiVerb;
13+
import org.jsondoc.sample.pojo.User;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.stereotype.Controller;
16+
import org.springframework.web.bind.annotation.PathVariable;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RequestMethod;
19+
import org.springframework.web.bind.annotation.ResponseBody;
20+
21+
@Api(name = "User Services", description = "Methods for managing users")
22+
@Controller
23+
@RequestMapping(value = "/users")
24+
public class UserController {
25+
26+
@ApiMethod(path = "/users/{id}", verb = ApiVerb.GET, description = "Gets a user with the given ID", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
27+
@ApiErrors(apierrors = { @ApiError(code = "3000", description = "User not found"), @ApiError(code = "9000", description = "Illegal argument") })
28+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
29+
public @ResponseBody @ApiResponseObject
30+
User user(@PathVariable @ApiParam(name = "id", description = "The user's ID", required = true) Integer id) {
31+
return new User(id, "jsondoc-user", 30, "M");
32+
}
33+
34+
@ApiMethod(path = "/users/{gender}/{age}", verb = ApiVerb.GET, description = "Gets users with the given gender and age", produces = { MediaType.APPLICATION_JSON_VALUE })
35+
@ApiErrors(apierrors = { @ApiError(code = "3000", description = "User not found"), @ApiError(code = "9000", description = "Illegal argument") })
36+
@RequestMapping(value = "/{gender}/{age}", method = RequestMethod.GET)
37+
public @ResponseBody @ApiResponseObject
38+
List<User> userAge(@PathVariable @ApiParam(name = "gender", description = "The user's gender", required = true) String gender, @PathVariable @ApiParam(name = "age", description = "The user's required age", required = true) Integer age) {
39+
List<User> users = new ArrayList<User>();
40+
users.add(new User(1, "jsondoc-user-1", age, gender));
41+
users.add(new User(2, "jsondoc-user-2", age, gender));
42+
return users;
43+
}
44+
45+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.jsondoc.sample.pojo;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlElement;
6+
import javax.xml.bind.annotation.XmlRootElement;
7+
8+
import org.jsondoc.core.annotation.ApiObject;
9+
import org.jsondoc.core.annotation.ApiObjectField;
10+
11+
@ApiObject(name = "user")
12+
@XmlRootElement
13+
@XmlAccessorType(XmlAccessType.FIELD)
14+
public class User {
15+
16+
@ApiObjectField(description = "The ID of the user")
17+
@XmlElement(name = "id")
18+
private Integer id;
19+
@ApiObjectField(description = "The username of the user")
20+
@XmlElement(name = "username")
21+
private String username;
22+
@ApiObjectField(description = "The age of the user")
23+
@XmlElement(name = "age")
24+
private Integer age;
25+
@ApiObjectField(description = "The gender of the user")
26+
@XmlElement(name = "gender")
27+
private String gender;
28+
29+
public User() {
30+
31+
}
32+
33+
public User(Integer id, String username, Integer age, String gender) {
34+
super();
35+
this.id = id;
36+
this.username = username;
37+
this.age = age;
38+
this.gender = gender;
39+
}
40+
41+
public Integer getId() {
42+
return id;
43+
}
44+
45+
public void setId(Integer id) {
46+
this.id = id;
47+
}
48+
49+
public String getUsername() {
50+
return username;
51+
}
52+
53+
public void setUsername(String username) {
54+
this.username = username;
55+
}
56+
57+
public Integer getAge() {
58+
return age;
59+
}
60+
61+
public void setAge(Integer age) {
62+
this.age = age;
63+
}
64+
65+
public String getGender() {
66+
return gender;
67+
}
68+
69+
public void setGender(String gender) {
70+
this.gender = gender;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)