Java – spring boot and Apache CXF for restful web services?
I'm part of the coding competition. My task is to create a restful online market where users can publish purchase and sale requests through HTTP
I need to build a front - end web service that receives and stores these requests
Technical requirements include spring start and CXF As far as I know, both CXF and spring boot can accept HTTP requests
In spring boot, you use a controller, such as:
@Controller @EnableAutoConfiguration public class controller { @RequestMapping("/") @ResponseBody String home() { return "Hello,World!"; } }
Using CXF (using javax. WS. RS), the code may be as follows:
@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 the two methods of handling HTTP requests? Is there a way to use both spring boot and CXF in the same application?
Solution
Spring MVC and Apache CXF are two separate frameworks to handle HTTP requests, which can be used to build rest web services
>Spring MVC is an umbrella project under spring (so it is closely combined with the spring framework), > Apache CXF is an open source implementation of jax-rs (rest) and jax-ws (soap) Apache CXF can run independently or be included in spring applications
If you are building a rest web service, they are mutually exclusive (you must choose one) If all you have to do is build rest web services, they are almost equivalent If you also need an MVC framework to provide HTML pages, spring MVC has this function (not supported by CXF)
Personal opinion: Spring MVC is easier to get started than CXF (requires more XML configuration) (thanks to spring boot for handling most configurations)
PS: in your CXF example, you have a @ WebService comment This annotation is part of Jax - WS (soap), not Jax - RS (rest) You may not need it