Emil
Emil

Reputation: 13789

Rest API on Struts

I'm currently adding rest services for a struts application.The struts that we use is 1.x .I came to know that rest plugin is only supported for struts2 and that too the plugin is not fully restful. In such a case, is rest plugin a good choice? Is it worth moving from Struts 1.x to 2 for rest plugin support? I googled about this topic and I could find very few resources on this topic.It would be really helpful to know how other have added rest services to their app.

Resources I have found:

The code in the second link is a bit bloated and won't scale for migrating a large Struts 1.x application.I have thought about using jersey but i have no idea how to use it along with struts 1.x .May be i'm doing it all wrong.If any one has some idea on developing restful apps please help.

Upvotes: 1

Views: 9593

Answers (2)

Sohail Anwar
Sohail Anwar

Reputation: 450

Using Jersey with struts 1 is good idea to implement Rest nature in project.

Few Easy steps we can implement jersey in struts 1

  • download jersey jar with all dependancies
  • add all jars to build path
  • modify web.xml to include rest nature

Below jars are compatible with java 8

  • jakarta.activation-1.2.1.jar
  • jakarta.activation-api-1.2.1.jar
  • jakarta.annotation-api-1.3.5.jar
  • jakarta.inject-2.6.1.jar
  • jakarta.validation-api-2.0.2.jar
  • jakarta.ws.rs-api-2.1.6.jar
  • jakarta.xml.bind-api-2.3.2.jar
  • jersey-client-2.29.1.jar
  • jersey-common-2.29.1.jar
  • jersey-container-servlet-2.29.1.jar
  • jersey-container-servlet-core-2.29.1.jar
  • jersey-media-jaxb-2.29.1.jar
  • jersey-server-2.29.1.jar
  • osgi-resource-locator-1.0.3.jar

Modify web.xml and new servlet mapping

<servlet> 
    <servlet-name>Rest</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
        <param-name>jersey.config.server.provider.packages</param-name> 
        <param-value>com.myorg.rest.controllers</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Rest</servlet-name> 
    <url-pattern>/rest/v1/*</url-pattern> 
</servlet-mapping> 

Now we can write rest controller and access it via ajax call

footnotes

Upvotes: 0

Will Hartung
Will Hartung

Reputation: 118691

Well, I'd move on to Struts 2 simply because Struts 1 is awful, but that's me.

Jersey will slot in quite handily alongside a Struts 1 app.

It is configured simply in web.xml, it will share the same session state with your Struts app, and doesn't really conflict at all.

Any logic pre-existing in your Struts app can be readily reused and repurposed with the most basic of refactoring. Worst you'd have to do is yank it out of the Struts actions (though they're pretty generic on their own, you may be able to use them directly).

Also, all the standard Servlet stuff will work fine with Jersey, like Filters and Sessions and what not. Since a lot of folks use Filters for security or setting up persistence and such, it should Just Work behind that existing infrastructure.

Just fire it up and put it in parallel and you should be good to go.

Upvotes: 3

Related Questions