Java – play! Is framework 2.0 suitable for creating rest APIs?

I have developed a rest API using play! Frame 1.2 4. I like the frame very much The simple and fast development cycle helps me achieve this. As long as I follow the traditional Java EE route, I will spend little time

Now I'm exploring using play! 2.0. 3 for my next project I see that although the framework has been enhanced and made it easier to develop web applications, the rest API can't say that My application will not have any HTML - I will only respond with XML or JSON or any data exchange format I decide to use in the future

So the question is:

More details:

Here's what I think in play 2.0 X and 1.2 X is more difficult than developing pure rest API If I am wrong, please correct my understanding

Content negotiation is more difficult

Playing! 1.2. 4. The content negotiation is established within the framework There are options to define the desired content type of the request in the routing file

GET / friends User. listFriends(format:’xml’)

Then, in the controller,

public static void getFriends(){
    render();
}

This will result in view / XML / user / listFriends Automatic rendering of XML templates In order to add JSON support tomorrow, all I need to do is add a views / JSON / user / listFriends JSON template

I can't see this can be playing! 2.0. X version

Creating non HTML templates is not intuitive

After some trial and error, I found that I could create a listFriends scala. A listFriends. XML scala. XML play! 2.0. Then, we need to call the controller code as follows:

return ok(views.xml.listFriends.render());

However, eclipse doesn't like this because eclipse doesn't know about views xml. ListFriends, because it is generated only after the playback compilation is completed Is there anything I missed here?

Solution

In play (Scala), you can do this:

val myXMl = obtainXML();
return Ok(myXML).as("text/xml")

I'm not sure about the syntax in Java, but it's equivalent: instead of creating a template, you generate XML, send it to the user, and set the return type to "text / XML" (or JSON or whatever you need)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>