我使用这个Jersey注释的Rest消费者POJO没有任何问题,但是我们不得不开始通过HTTPS部署我们的应用程序。 我得到了一个大胖404,我得出结论,其他一切都在代码中运行(它运行在MULE上非常漂亮)并且我的POJO根本不消耗HTTPS请求。 我怀疑
我添加了一个非常小的注释和voiala!都好。 显然,希望2.在这里。 这是代码:
package org.acme.api;
import java.net.UnknownHostException; import java.util.List;
import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.POST; import javax.ws.rs.PathParam; import javax.ws.rs.HeaderParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBList; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import com.mongodb.ServerAddress; import com.mongodb.DBObject; import com.mongodb.WriteResult; import com.mongodb.util.JSON; import com.mongodb.DB;
/ **
@Path( “/”)
公共类产品{
MongoClient client = null; DB db = null; DBCollection products = null;
public Product()抛出UnknownHostException { this.client = new MongoClient(new ServerAddress(“localhost”,27027)); this.db = this.client.getDB(“dailysale”); this.products = this.db.getCollection(“products”); }
@GET @Produces( “应用程序/ JSON”) @Path( “/产物/ {的productId}”) public String retrieveProduct(@PathParam(“productId”)Integer productId){
String product;
try{
DBObject query = new BasicDBObject("productId", String.valueOf(productId));
product = this.products.findOne(query).toString();
} catch(Exception e) {
product = "{ error: 'We were not able to find product with Id: '" + productId + ". " + e.getLocalizedMessage() + " }";
}
return product;
}
@GET @Produces( “应用程序/ JSON”) @Path( “/产物/网格/ {查询}”) @HeaderParam( “范围”) public Response listProducts(@HeaderParam(“range”)String range,@ PathParam(“query”)String query){
Integer pageNum = 1;
Integer pageSize = 10;
Integer resultCount = null;
BasicDBList result = new BasicDBList();
Integer firstItem = (pageNum - 1 ) * pageSize;
Long allProductsCount = null;
Integer lastItem = null;
DBObject searchQuery = new BasicDBObject("$regex", query);
try {
DBCursor productListCursor = products.find(searchQuery).skip(firstItem).limit( pageSize );
List<DBObject> productList = productListCursor.toArray();
for(DBObject product : productList) {
result.add(this.itemToGridConverter(product));
}
resultCount = productList.size();
allProductsCount = products.count();
lastItem = firstItem + resultCount - 1;
} catch(Exception e) {
result = new BasicDBList();
result.add(new BasicDBObject("error", "We were not able to retrieve all products with pageSize: " + pageSize + " and pageNumber " + pageNum + ". " + e.getLocalizedMessage() ));
}
return Response.ok(result).header("Content-Range", "items " + firstItem + "-" + lastItem + "/" + allProductsCount).build();
}
@GET @Produces( “应用程序/ JSON”) @Path( “/产物/格/”) @HeaderParam( “范围”) public Response listProducts(@HeaderParam(“range”)String range){
Integer pageNum = 1;
Integer pageSize = 10;
Integer resultCount = null;
BasicDBList result = new BasicDBList();
Integer firstItem = (pageNum - 1 ) * pageSize;
Long allProductsCount = null;
Integer lastItem = null;
try {
DBCursor productListCursor = products.find().skip(firstItem).limit( pageSize ).sort( new BasicDBObject( "sku", 1)) ;
List<DBObject> productList = productListCursor.toArray();
for(DBObject product : productList) {
result.add(this.itemToGridConverter(product));
}
resultCount = productList.size();
allProductsCount = products.count();
lastItem = firstItem + resultCount - 1;
} catch(Exception e) {
result = new BasicDBList();
result.add(new BasicDBObject("error", "We were not able to retrieve all products with pageSize: " + pageSize + " and pageNumber " + pageNum + ". " + e.getLocalizedMessage() ));
}
return Response.ok( result ).header("Content-Range", "items " + firstItem + "-" + lastItem + "/" + allProductsCount).build();
}
@GET @Produces( “应用程序/ JSON”) @Path( “/产物/网格/ {排序}”) @HeaderParam( “范围”) public Response listProductsWithSort(@HeaderParam(“range”)String range,@ PathParam(“sort”)String sorting){
Response response = null;
return response;
}
@PUT @Produces( “应用程序/ JSON”) @Path( “/产物”) public String createProduct(String productJson){ DBObject product = null; 字符串结果; 尝试{ product =(DBObject)JSON.parse(productJson); WriteResult wResult = this.products.insert(product); result = wResult.toString(); } catch(例外e){ result =“{error:'我们无法插入产品。' “+ e.getLocalizedMessage()+”}“; }
return result;
}
@POST @Produces( “应用程序/ JSON”) @Path( “/产物/ {的productId}”) public String updateProduct(@PathParam(“productId”)Integer productId,String productJson){ DBObject product = null; DBObject query = new BasicDBObject(); String result = null; 尝试{ product =(DBObject)JSON.parse(productJson); query.put(“productId”,productId.toString()); WriteResult wResult = this.products.update(query,product); result = wResult.toString(); } catch(例外e){ result =“{error:'我们无法更新产品。' “+ e.getLocalizedMessage()+”}“; }
return result;
}
@DELETE @Produces( “应用程序/ JSON”) @Path( “/产物/ {的productId}”) public String deleteProduct(@PathParam(“productId”)Integer productId){
return "{ error: 'This function is not implemented [delete].' }";
}
private DBObject itemToGridConverter(DBObject product){ DBObject item = new BasicDBObject(); BasicDBList images =(BasicDBList)product.get(“images”); BasicDBObject firstImage =(BasicDBObject)images.get(0);
item.put("productId", product.get("productId"));
item.put("sku", product.get("sku"));
item.put("image", firstImage.get("imageurl"));
BasicDBList spec = (BasicDBList)product.get("productSpecifics");
BasicDBObject firstSpec = (BasicDBObject)spec.get(0);
BasicDBObject attributes = (BasicDBObject)product.get("productAttributes");
item.put("name", firstSpec.get("title"));
item.put("cost", "no source");
item.put("list_price", attributes.get("defaultsalePrice"));
item.put("qty_available", product.get("qtyonHand"));
item.put("condition", "no source");
item.put("active", "true".equals(product.get("active")) ? "Yes" : "No");
item.put("manufacturer", attributes.get("manufacturer"));
item.put("part_number", attributes.get("manufacturerPartNumber"));
item.put("date_created", product.get("_id"));
return item;
}
}
非常感谢。
更新
我通过修复路径修复了问题。问题是MULE如何解析范围边界,这里原始的@Path /实际上是在前一个范围内。现在我们改变了流程,@ Path变成了/ controllers /。 Uyodiin Mevin。