我继承了一个承包商启动的网络项目。我和我的同事不熟悉所使用的技术,并且有很多问题。据我们所知,这似乎是某种 RESTful Java 服务器代码,但我的理解是有很多不同类型的 Java RESTful 服务。这是哪一个?具体问题:
1) 我们可以在哪里阅读有关此特定服务的更多信息(特别是介绍性信息)?
2) 代码通过某种“魔法”创建并返回 JSON...我只返回一个模型类(下面的代码),该类的字段具有 getter 和 setter 方法,并且它会自动转换为 JSON。我想详细了解这是如何自动完成的。
3) 我们已经有一些创建 JSON 的代码。我们需要使用这个框架返回它。如果我已经有一个 JSON,我该如何返回它?我尝试过这样的事情:
String testJSON = "{\"菜单\": {\"id\":\"文件\",\"值\":\"你好\"}}"; 返回测试JSON;
不是返回带有 getter/setter 的模型对象,而是返回文字文本字符串,而不是 JSON。有没有办法返回一个已经是 JSON 字符串的实际 JSON,并将其作为 JSON 发送?
您不必能够回答上述所有问题。任何/所有有帮助的方向的指示表示赞赏!
代码
首先,返回 JSON 的 View Controller :
package com.aimcloud.server;
import com.aimcloud.util.MySqlConnection;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.aimcloud.models.SubscriptionTierModel;
@Path("subscription_tier")
public class SubscriptionTierController
{
// this method will return a list of subscription_tier table entries that are currently active
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String/*ArrayList<SubscriptionTierModel>*/ getSubscriptionTiers(@QueryParam("includeActiveOnly") Boolean includeActiveOnly)
{
MySqlConnection mysql = MySqlConnection.getConnection();
ArrayList<SubscriptionTierModel> subscriptionTierArray = new ArrayList<SubscriptionTierModel>();
String queryString;
if (includeActiveOnly)
queryString = "SELECT * FROM subscription_tier WHERE active=1";
else
queryString = "SELECT * FROM subscription_tier";
List<Map<String, Object>> resultList = mysql.query(queryString, null);
for (Map<String, Object> subscriptionRow : resultList)
subscriptionTierArray.add( new SubscriptionTierModel(subscriptionRow) );
// String testJSON = "{\"menu\": {\"id\": \"file\", \"value\": \"Hello there\"}}";
// return testJSON;
return subscriptionTierArray;
}
}
接下来,上面的代码返回的模型:
package com.aimcloud.models;
// NOTE this does NOT import Globals
import java.sql.Types;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import com.aimcloud.util.LoggingUtils;
public class SubscriptionTierModel extends ModelPrototype
{
private String name;
private Integer num_studies;
private Integer cost_viewing;
private Integer cost_processing;
private Integer active;
protected void setupFields()
{
this.fields.add("name");
this.fields.add("num_studies");
this.fields.add("cost_viewing");
this.fields.add("cost_processing");
this.fields.add("active");
}
public SubscriptionTierModel()
{
super("subscription");
this.setupFields();
}
public SubscriptionTierModel(Map<String, Object> map)
{
super("subscription");
this.setupFields();
this.initFromMap(map);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setNum_Studies(Integer num_studies) {
this.num_studies = num_studies;
}
public Integer getNum_studies() {
return this.num_studies;
}
public void setCost_viewing(Integer cost_viewing) {
this.cost_viewing = cost_viewing;
}
public Integer getCost_viewing() {
return this.cost_viewing;
}
public void setCost_processing(Integer cost_processing) {
this.cost_processing = cost_processing;
}
public Integer getCost_processing() {
return this.cost_processing;
}
public void setActive(Integer active) {
this.active = active;
}
public Integer getActive() {
return this.active;
}
}
public abstract class ModelPrototype {
protected MySqlConnection mysql;
protected ArrayList<String> fields;
protected String table;
protected Integer id = null;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
abstract protected void setupFields();
public ModelPrototype() {
mysql = MySqlConnection.getConnection();
this.fields = new ArrayList<String>();
this.fields.add("id");
}
public void initFromDbResult(List<Map<String, Object>> result) {
if (result.size() >= 1)
{
Map<String, Object> userRow = result.get(0);
this.initFromMap(userRow);
if (result.size() > 1)
{
Thread.dumpStack();
}
}
else
{
throw new WebApplicationException(ServerUtils.generateResponse(Response.Status.NOT_FOUND, "resource not found"));
}
}
protected void initFromMap(Map<String, Object> map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object value = entry.getValue();
// LoggingUtils.log(entry.getKey() + " " + entry.getValue().toString());
if (value != null && this.fields.contains(entry.getKey())) {
this.setField(entry.getKey(), value);
}
}
}
....
最佳答案
1) Where can we read more (particularly introductory information) about this specific service?
这是一个 RESTful 服务,它使用基本的 jax-rs 注释来构建服务。我建议查看“REST using jersey”或“REST using CXF”等教程。
2) The code creates and returns a JSON through some kind of "magic"...
使用的restful框架通常会处理这个问题。 @Produces({ MediaType.APPLICATION_JSON })
注释指示执行此转换的框架。这将在配置中的某个位置定义。如果您使用 spring 来定义 bean,请检查 spring 配置文件。通常会定义一个映射器或提供者来将对象转换为 json。
3) 我们已经有一些创建 JSON 的代码。我们需要使用这个框架返回它。如果我已经有一个 JSON,我该如何返回它?我尝试过这样的事情:
如果您已经有一个 json,只需从该方法返回该 json。请记住该方法上仍然有 @Produces({ MediaType.APPLICATION_JSON })
注释。
but this returns a literal text string, not a JSON
json 是一个字符串。这就是您将在响应中看到的内容,除非您将其反序列化回对象。
关于rest - 从 RESTful Java 服务器代码返回 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17712188/