I'm new to dotCMS (actually I evaluat it for a project).
I created a custom type, for the sake of simplicity we can pretend that it's just a Message with a WYSIWYG content field.
Now I want to display this message in a web client app using the rest API.
So I get my messages with :
/api/content/query/+structureName:Message
and I got this (which is perfect) :
"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",
...
}
]
Now I'm missing the owner public name and I can't get it with the standard rest API. How am I supposed to do this ?
The closer I get is using the osgi plugin based on the spring example. And reading the sourcecode of the com.dotcms.rest package. I try to get it at app/spring/myController/test or app/spring/myController/getUserNameById/id/dotcms.org.2831 but no success (EDIT: I got a 404 not found and no errors in logs). Known issue are in the 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());
}
}
Use the Jersey osgi example provided. It allows you to extend and add new endpoints to the dotCMS rest API. See:
https://github.com/dotCMS/core/tree/master-3.2/docs/examples/osgi/com.dotcms.rest