球衣自定义 JSON 回复
Jersey custom JSON responses
我正在使用 Jersey 设计 Web 服务,我需要向我的 JSON 响应添加一些自定义字段,类似于:
{
"result": "0"/"Error code",
"message": "Message returned by server",
"custom_field": here goes string or class values required
}
比如我有Token和User POJO类:
public class Token {
private String token = null;
public Token (String token) {
this.token = token;
}
// getter and setter
}
public class User {
private String id= null;
private String name = null;
private String surname = null;
private String phone = null;
public User (String id, String name, String surname, String phone) {
this.id = id;
this.name = name;
this.surname = surname;
this.phone = phone;
}
// getters and setters
}
这是登录和获取用户网络服务:
@Path("/services")
@Produces(MediaType.APPLICATION_JSON)
public class HelloFromCxfRestService {
@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Token login(String login, String password){
//Logic
return token;
}
@POST
@Path("/get_user")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String getUser (String id){
//Logic
return user; //in custom_field would be {"name":"myName", "surname":"mySurname", "phone":"myPhone"}
}
}
如何 return 我的自定义 JSON 结构尊重 POJO?显然我应该可以将自定义值放入 "result" 和 "message".
创建 Response
class:
public class Response<T> {
String result;
String message;
T customField;
public Response(String result, String message, T customField) {
this.result = result;
this.message = message;
this.customField = customField;
}
//getter setter
}
然后在控制器中:
@POST
@Path("/get_user")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response<User> getUser (String id){
//Logic
return new Response<User> ("result", "message", user);
}
在客户端你可以使用下面的代码:
public class WSClient {
private URI uri;
private Client client;
public WSClient(){
uri = UriBuilder.fromUri("pathToYoutWebservice").port(8080).build();
client = ClientBuilder.newClient();
}
public User getUser(String userId){
User user = client.target(uri).path(java.text.MessageFormat.format("{0}", new Object[]{userId})).request(MediaType.APPLICATION_JSON).get(User.class);
return user;
}
}
我正在使用 Jersey 设计 Web 服务,我需要向我的 JSON 响应添加一些自定义字段,类似于:
{
"result": "0"/"Error code",
"message": "Message returned by server",
"custom_field": here goes string or class values required
}
比如我有Token和User POJO类:
public class Token {
private String token = null;
public Token (String token) {
this.token = token;
}
// getter and setter
}
public class User {
private String id= null;
private String name = null;
private String surname = null;
private String phone = null;
public User (String id, String name, String surname, String phone) {
this.id = id;
this.name = name;
this.surname = surname;
this.phone = phone;
}
// getters and setters
}
这是登录和获取用户网络服务:
@Path("/services")
@Produces(MediaType.APPLICATION_JSON)
public class HelloFromCxfRestService {
@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Token login(String login, String password){
//Logic
return token;
}
@POST
@Path("/get_user")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String getUser (String id){
//Logic
return user; //in custom_field would be {"name":"myName", "surname":"mySurname", "phone":"myPhone"}
}
}
如何 return 我的自定义 JSON 结构尊重 POJO?显然我应该可以将自定义值放入 "result" 和 "message".
创建 Response
class:
public class Response<T> {
String result;
String message;
T customField;
public Response(String result, String message, T customField) {
this.result = result;
this.message = message;
this.customField = customField;
}
//getter setter
}
然后在控制器中:
@POST
@Path("/get_user")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response<User> getUser (String id){
//Logic
return new Response<User> ("result", "message", user);
}
在客户端你可以使用下面的代码:
public class WSClient {
private URI uri;
private Client client;
public WSClient(){
uri = UriBuilder.fromUri("pathToYoutWebservice").port(8080).build();
client = ClientBuilder.newClient();
}
public User getUser(String userId){
User user = client.target(uri).path(java.text.MessageFormat.format("{0}", new Object[]{userId})).request(MediaType.APPLICATION_JSON).get(User.class);
return user;
}
}