gpt4 book ai didi

java - 扩展 dotCMS REST API

转载 作者:行者123 更新时间:2023-11-30 03:27:43 25 4
gpt4 key购买 nike

我是 dotCMS 的新手(实际上我是为了一个项目评估它)。

我创建了一个自定义类型,为了简单起见,我们可以假装它只是一 strip 有所见即所得内容字段的消息。

现在我想使用 REST API 在 Web 客户端应用程序中显示此消息。

所以我收到的消息是:

/api/content/query/+structureName:Message

我得到了这个(这是完美的):

"contentlets":[{
"owner":"dotcms.org.2831",
"content": "<p>Hi !<\/p>\n<p>I am a <strong>Message<\/strong>.<\/p>\n<p>See you.<\/p>",
...
},{
"owner":"dotcms.org.2831",
...
}
]

现在我缺少所有者公共(public)名称,并且无法使用标准 REST API 获取它。我该怎么做?

我更接近的是使用基于 spring 示例的 osgi 插件。并阅读 com.dotcms.rest 包的源代码。我尝试在 app/spring/myController/test 或 app/spring/myController/getUserNameById/id/dotcms.org.2831 获取它,但没有成功(编辑:我找不到 404 并且没有日志中的错误)。已知问题位于 javadoc 中:

@EnableWebMvc
@Configuration
@RequestMapping ("/myController")
@Controller
/**
* I also try with @Path("/myController") but I got a 404 not found.
**/
public class myController extends WebResource {

/**
* This default method works fine.
**/
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView helloWorld() {
Logger.info( this.getClass(), "Received request to hello" );
String myMessage = "Hello World, Spring 3.1 and dotCMS!";
// This will resolve to /application/spring/helloworld.dot
return new ModelAndView("helloworld", "message", myMessage);
}

/**
* This one dosen't accept my MappingJacksonJsonView. But no error in the logfile.
**/
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView test() {
Logger.info( this.getClass(), "Test request" );
Map<String,String> model = new HashMap<>();
model.put("key", "value");
Logger.info( this.getClass(), "Map ready" );
MappingJacksonJsonView view = new MappingJacksonJsonView();
Logger.info( this.getClass(), "View ready" );
ModelAndView modelAndView = new ModelAndView(view, model);
Logger.info( this.getClass(), "Response ready" );
return modelAndView;
}

/**
* I can't reach this method. And if so, will the Response return type even work ?
**/
@GET
@Path("/getUserNameById/{params:.*}")
@Produces("application/json")
public Response getUserById(@Context HttpServletRequest request, @PathParam("params") String params) throws JSONException {

InitDataObject initData = init( params, true, request, true );

Map<String, String> paramsMap = initData.getParamsMap();
String userId = paramsMap.get("id");
Logger.info( this.getClass(), "Received request with userId " + userId );

//Creating an utility response object
ResourceResponse responseResource = new ResourceResponse( initData.getParamsMap() );

JSONObject jsonObject = new JSONObject();
jsonObject.put("userId", userId);
//Get and put user public name into the jsonObject here.
Logger.info( this.getClass(), jsonObject.toString());
return responseResource.response(jsonObject.toString());
}
}

最佳答案

使用提供的 Jersey osgi 示例。它允许您扩展并添加新端点到 dotCMS Rest API。请参阅:

https://github.com/dotCMS/core/tree/master-3.2/docs/examples/osgi/com.dotcms.rest

关于java - 扩展 dotCMS REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29794568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com