{"id":5008,"date":"2017-05-29T18:43:49","date_gmt":"2017-05-29T18:43:49","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5008"},"modified":"2020-11-11T18:57:03","modified_gmt":"2020-11-11T18:57:03","slug":"building-a-rest-service-with-spring","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/building-a-rest-service-with-spring\/","title":{"rendered":"Building a REST service with Spring"},"content":{"rendered":"<p>I&#8217;ve been doing a fair bit with SOAP, REST and Java recently using CXF. A previous post demonstrated using CXF\/JAXB to create a REST style service that returned either JSON or XML, let&#8217;s look at using <a href=\"https:\/\/spring.io\/guides\/gs\/rest-service\/\" target=\"_blank\" rel=\"noopener noreferrer\">Spring REST services<\/a> to implement the same functionality.<\/p>\n<p>Let&#8217;s start (as usual) with our pom.xml<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n    xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;\r\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\r\n    &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;sample-rest-service&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.1.0&lt;\/version&gt;\r\n\r\n    &lt;parent&gt;\r\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\r\n        &lt;version&gt;1.5.2.RELEASE&lt;\/version&gt;\r\n    &lt;\/parent&gt;\r\n\r\n    &lt;dependencies&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\r\n            &lt;scope&gt;test&lt;\/scope&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;com.jayway.jsonpath&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;json-path&lt;\/artifactId&gt;\r\n            &lt;scope&gt;test&lt;\/scope&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n\r\n    &lt;properties&gt;\r\n        &lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n    &lt;\/properties&gt;\r\n\r\n\r\n    &lt;build&gt;\r\n        &lt;plugins&gt;\r\n            &lt;plugin&gt;\r\n                &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n                &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\r\n            &lt;\/plugin&gt;\r\n        &lt;\/plugins&gt;\r\n    &lt;\/build&gt;\r\n\r\n    &lt;repositories&gt;\r\n        &lt;repository&gt;\r\n            &lt;id&gt;spring-releases&lt;\/id&gt;\r\n            &lt;url&gt;https:\/\/repo.spring.io\/libs-release&lt;\/url&gt;\r\n        &lt;\/repository&gt;\r\n    &lt;\/repositories&gt;\r\n    &lt;pluginRepositories&gt;\r\n        &lt;pluginRepository&gt;\r\n            &lt;id&gt;spring-releases&lt;\/id&gt;\r\n            &lt;url&gt;https:\/\/repo.spring.io\/libs-release&lt;\/url&gt;\r\n        &lt;\/pluginRepository&gt;\r\n    &lt;\/pluginRepositories&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>Now we create our folder structure, so create the following from the root folder<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nsrc\/main\/java\/demo\r\n<\/pre>\n<p>We&#8217;re going to need to code within the <em>demo<\/em> package.<\/p>\n<p><em>Without the package you might see a warning &#8220;Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.&#8221;<\/em><\/p>\n<p>Next up, we&#8217;ll create a service, or in Spring parlance, the controller<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage demo;\r\n\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n\r\n@RestController\r\npublic class SampleController {\r\n\r\n    @RequestMapping(&quot;\/purchaseOrder&quot;)\r\n    public PurchaseOrderType getPurchaseOrder() {\r\n        return new PurchaseOrderType(1234);\r\n    }\r\n}\r\n<\/pre>\n<p>The PurchaseOrderType is simply the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage demo;\r\n\r\npublic class PurchaseOrderType {\r\n\r\n    private long _id;\r\n\r\n    public PurchaseOrderType(long id) {\r\n        _id = id;\r\n    }\r\n\r\n    public long getId() {\r\n        return _id;\r\n    }\r\n}\r\n<\/pre>\n<p>Now we need the application to start the service<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage demo;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\n\r\n@SpringBootApplication\r\npublic class Application {\r\n\r\n    public static void main(String&#x5B;] args) {\r\n        SpringApplication.run(Application.class, args);\r\n    }\r\n}\r\n<\/pre>\n<p>To run this we can, ofcourse, set up a configuration or from the Terminal window run<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nmvn spring-boot:run\r\n<\/pre>\n<p>Now, if we navigate to <em>http:\/\/localhost:8080\/purchaseOrder\/<\/em> we should see<\/p>\n<p>{&quot;id&quot;:1234}<\/p>\n<p>Now, let&#8217;s extend this to allow us to return XML. We&#8217;ll need to annotate PurchaseOrderType with the @XmlRootElement, we&#8217;ll also need a default constructor and a setter for our Id property<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage demo;\r\n\r\nimport javax.xml.bind.annotation.XmlRootElement;\r\n\r\n@XmlRootElement\r\npublic class PurchaseOrderType {\r\n\r\n    private long _id;\r\n\r\n    public PurchaseOrderType() {\r\n    }\r\n\r\n    public PurchaseOrderType(long id) {\r\n        _id = id;\r\n    }\r\n\r\n    public long getId() {\r\n        return _id;\r\n    }\r\n\r\n    public void setId(long id) {\r\n        _id = id;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Changing the port of the Tomcat server<\/strong><\/p>\n<p>You may have noticed that when running this application we get an instance of a Tomcat server running, by default it uses port 8080, but what about if we need\/want to change this. We can create an EmbeddedServletContainerCustomizer, as below.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage demo;\r\n\r\nimport org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;\r\nimport org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class ApplicationContainer implements EmbeddedServletContainerCustomizer {\r\n\r\n    @Override\r\n    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {\r\n        configurableEmbeddedServletContainer.setPort(8888);\r\n    }\r\n}\r\n<\/pre>\n<p>Code is available here https:\/\/github.com\/putridparrot\/blog-projects\/tree\/master\/spring-rest<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing a fair bit with SOAP, REST and Java recently using CXF. A previous post demonstrated using CXF\/JAXB to create a REST style service that returned either JSON or XML, let&#8217;s look at using Spring REST services to implement the same functionality. Let&#8217;s start (as usual) with our pom.xml &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;project [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161,164],"tags":[],"class_list":["post-5008","post","type-post","status-publish","format-standard","hentry","category-java","category-rest"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5008"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5008\/revisions"}],"predecessor-version":[{"id":8616,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5008\/revisions\/8616"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}