Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot vs. Apache CXF for RESTful Web Services?

I am part of a coding competition, the task is to create a RESTful online marketplace where users can post buy and sell requests via http.

I need to build a front end web service that accepts and stores these requests.

The tech requirements include both Spring boot and CXF. As far as I am aware, both CXF and Spring boot are capable of accepting http requests.

In spring boot, you use a controller like:

@Controller
@EnableAutoConfiguration
public class controller {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello, World!";
    }
}

Whereas with CXF (using javax.ws.rs), the code might look like this:

@WebService(serviceName = "MarketService", targetNamespace = "http://localhost:9005")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public interface MarketService {

    @GET
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces({ MediaType.APPLICATION_JSON })
    @Path("/sells/{id}")
    public prod getProduct(@PathParam("id") int id);

Can someone help me understand the fundamental difference between these two approaches to handling http requests? Is there a way I can use both Spring Boot and CXF in the same application?

like image 458
user3899879 Avatar asked Aug 01 '14 14:08

user3899879


People also ask

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

Is Apache CXF a JAX-RS?

JAX-RS: Java API for RESTful Web Services is a Java programming language API that provides support in creating web services according to the Representational State Transfer (REST) architectural style. CXF supports JAX-RS 2.1 (JSR-370), 2.0 (JSR-339) and 1.1 (JSR-311).

What is spring CXF?

The application context loads Spring elements defined in a configuration file. In this case, the name of the servlet is cxf, therefore the context looks for those elements in a file named cxf-servlet. xml by default. Lastly, the CXF servlet is mapped to a relative URL: dispatcher.addMapping("/services");

Does spring boot use JAX-RS?

The spring-boot-starter-jersey is a starter for building RESTful web applications using JAX-RS and Jersey. It is an alternative to spring-boot-starter-web . The spring-boot-starter-test is a starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito.

What is Apache CXF support for spring?

This tutorial illustrated Apache CXF support for Spring. In particular, it has been shown how a web service may be published using a Spring configuration file, and how a client may interact with that service through a proxy created by an Apache CXF proxy factory, which was declared in another configuration file.

Should I use CXF or Spring MVC for web services?

If you are looking to build a REST web service, they are pretty much mutually exclusive (you have to pick one). If all you're going to do is build REST web services, then they're pretty much equivalent. If you also need to have an MVC framework to serve HTML pages, then Spring MVC has that capability (CXF does not).

What is CXF servlet in Spring Boot?

The application context loads Spring elements defined in a configuration file. In this case, the name of the servlet is cxf, therefore the context looks for those elements in a file named cxf-servlet.xml by default. Lastly, the CXF servlet is mapped to a relative URL: 3.2. The Good Old web.xml

What are the required bean types for Apache CXF?

The first required bean is the SpringBus – which supplies extensions for Apache CXF to work with the Spring Framework: An EnpointImpl bean also needs to be created using the SpringBus bean and a web service implementor. This bean is used to publish the endpoint at the given HTTP address:


2 Answers

Spring MVC and Apache CXF are 2 separate frameworks to handle HTTP requests and that can be used to build REST web services.

  • Spring MVC is a project under the Spring "umbrella" (and therefore strongly tied to the Spring framework on top of which it's built),
  • Apache CXF is a open-source implementation of JAX-RS (REST) and JAX-WS (SOAP). Apache CXF can be run standalone or be included in a Spring application.

If you are looking to build a REST web service, they are pretty much mutually exclusive (you have to pick one). If all you're going to do is build REST web services, then they're pretty much equivalent. If you also need to have an MVC framework to serve HTML pages, then Spring MVC has that capability (CXF does not).

Personal opinion: Spring MVC is easier to get started with (thanks to Spring Boot which handles most of the configuration for you) than CXF (which requires more XML configuration).

PS: in your CXF example, you have a @WebService annotation. This annotation is part of JAX-WS (SOAP), not JAX-RS (REST). You probably don't need it.

like image 72
Christophe L Avatar answered Sep 16 '22 14:09

Christophe L


Use the Spring Boot CXF JAX-RS starter by adding:

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
  <version>3.1.7</version>
</dependency>

See also: http://cxf.apache.org/docs/springboot.html

like image 21
Dennis Kieselhorst Avatar answered Sep 19 '22 14:09

Dennis Kieselhorst