├── .cfignore ├── .gitignore ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── MAINTAINERS.md ├── README.md ├── doc └── source │ └── images │ └── architecture.png ├── pom.xml └── src ├── main ├── java │ ├── application │ │ ├── ApiException.java │ │ ├── ApiOriginFilter.java │ │ ├── ApiResponseMessage.java │ │ ├── NotFoundException.java │ │ ├── ProductApi.java │ │ ├── ProductApiBinding.java │ │ ├── ProductApiController.java │ │ ├── ProductsApi.java │ │ ├── ProductsApiBinding.java │ │ ├── ProductsApiController.java │ │ ├── SBApplication.java │ │ ├── model │ │ │ └── Product.java │ │ └── rest │ │ │ └── HealthEndpoint.java │ └── io │ │ └── swagger │ │ ├── Info.java │ │ ├── RFC3339DateFormat.java │ │ └── configuration │ │ └── SwaggerDocumentationConfig.java └── resources │ ├── application.properties │ └── public │ └── index.html └── test └── java └── application └── HealthEndpointTest.java /.cfignore: -------------------------------------------------------------------------------- 1 | /.classpath 2 | /.project 3 | /.settings 4 | /src/main/resources/application-local.properties 5 | target/ 6 | build/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.m2 3 | 4 | 5 | /.classpath 6 | /.project 7 | /.settings 8 | 9 | /caches 10 | /local.properties 11 | .*.swp 12 | .DS_Store 13 | /src/main/resources/application-local.properties 14 | 15 | src/main/resources/application-local.properties 16 | src/main/resources/localdev-config.json -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | This is an open source project, and we appreciate your help! 4 | 5 | We use the GitHub issue tracker to discuss new features and non-trivial bugs. 6 | 7 | In addition to the issue tracker, [#journeys on 8 | Slack](https://dwopen.slack.com) is the best way to get into contact with the 9 | project's maintainers. 10 | 11 | To contribute code, documentation, or tests, please submit a pull request to 12 | the GitHub repository. Generally, we expect two maintainers to review your pull 13 | request before it is approved for merging. For more details, see the 14 | [MAINTAINERS](MAINTAINERS.md) page. 15 | 16 | Contributions are subject to the [Developer Certificate of Origin, Version 1.1](https://developercertificate.org/) and the [Apache License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.txt). 17 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ibmjava:8-sfj 2 | LABEL maintainer="IBM Java Engineering at IBM Cloud" 3 | 4 | COPY target/springbackendforfrontend-1.0-SNAPSHOT.jar /app.jar 5 | 6 | ENV JAVA_OPTS="" 7 | ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | # Maintainers Guide 2 | 3 | This guide is intended for maintainers - anybody with commit access to one or 4 | more Code Pattern repositories. 5 | 6 | ## Methodology 7 | 8 | This repository does not have a traditional release management cycle, but 9 | should instead be maintained as a useful, working, and polished reference at 10 | all times. While all work can therefore be focused on the master branch, the 11 | quality of this branch should never be compromised. 12 | 13 | The remainder of this document details how to merge pull requests to the 14 | repositories. 15 | 16 | ## Merge approval 17 | 18 | The project maintainers use LGTM (Looks Good To Me) in comments on the pull 19 | request to indicate acceptance prior to merging. A change requires LGTMs from 20 | two project maintainers. If the code is written by a maintainer, the change 21 | only requires one additional LGTM. 22 | 23 | ## Reviewing Pull Requests 24 | 25 | We recommend reviewing pull requests directly within GitHub. This allows a 26 | public commentary on changes, providing transparency for all users. When 27 | providing feedback be civil, courteous, and kind. Disagreement is fine, so long 28 | as the discourse is carried out politely. If we see a record of uncivil or 29 | abusive comments, we will revoke your commit privileges and invite you to leave 30 | the project. 31 | 32 | During your review, consider the following points: 33 | 34 | ### Does the change have positive impact? 35 | 36 | Some proposed changes may not represent a positive impact to the project. Ask 37 | whether or not the change will make understanding the code easier, or if it 38 | could simply be a personal preference on the part of the author (see 39 | [bikeshedding](https://en.wiktionary.org/wiki/bikeshedding)). 40 | 41 | Pull requests that do not have a clear positive impact should be closed without 42 | merging. 43 | 44 | ### Do the changes make sense? 45 | 46 | If you do not understand what the changes are or what they accomplish, ask the 47 | author for clarification. Ask the author to add comments and/or clarify test 48 | case names to make the intentions clear. 49 | 50 | At times, such clarification will reveal that the author may not be using the 51 | code correctly, or is unaware of features that accommodate their needs. If you 52 | feel this is the case, work up a code sample that would address the pull 53 | request for them, and feel free to close the pull request once they confirm. 54 | 55 | ### Does the change introduce a new feature? 56 | 57 | For any given pull request, ask yourself "is this a new feature?" If so, does 58 | the pull request (or associated issue) contain narrative indicating the need 59 | for the feature? If not, ask them to provide that information. 60 | 61 | Are new unit tests in place that test all new behaviors introduced? If not, do 62 | not merge the feature until they are! Is documentation in place for the new 63 | feature? (See the documentation guidelines). If not do not merge the feature 64 | until it is! Is the feature necessary for general use cases? Try and keep the 65 | scope of any given component narrow. If a proposed feature does not fit that 66 | scope, recommend to the user that they maintain the feature on their own, and 67 | close the request. You may also recommend that they see if the feature gains 68 | traction among other users, and suggest they re-submit when they can show such 69 | support. 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WARNING: This repository is no longer maintained :warning: 2 | 3 | > This repository will not be updated. The repository will be kept available in read-only mode. 4 | 5 | [![](https://img.shields.io/badge/IBM%20Cloud-powered-blue.svg)](https://cloud.ibm.com) 6 | [![Platform](https://img.shields.io/badge/platform-java-lightgrey.svg?style=flat)](https://www.ibm.com/developerworks/learn/java/) 7 | 8 | # Create and deploy a Java Backend For Frontend (BFF) using Spring 9 | 10 | > We have similar patterns available for [Node.js](https://github.com/IBM/nodejs-backend-for-frontend), [Swift](https://github.com/IBM/swift-backend-for-frontend), and [Java Liberty](https://github.com/IBM/java-liberty-backend-for-frontend) as well! 11 | 12 | In this code pattern, you will create a Backend for Frontend (BFF) web service using [Spring](https://spring.io/) in Java, matching a RESTful API documented in [Swagger](https://swagger.io/). 13 | 14 | A BFF can be used to elegantly expose complex backend systems to multiple client-facing platforms, such as iOS and the web, without building a monolothic API that attempts to serve all clients equally. Different types of clients require different types of user experiences, and having a backend API tightly coupled to each specific user experience actually makes for a much more loosely coupled system overall. This pattern helps frontend teams iterate on features faster by giving them more control over the APIs they interact with, without affecting the user experience or development team driving a sister frontend. 15 | 16 | When you have completed this code pattern, you will understand how to: 17 | 18 | * Build out the Backend for Frontend (BFF) architecture pattern 19 | * Deploy to Kubernetes, Cloud Foundry or a DevOps Pipeline 20 | * Operate an application with monitoring and distributed trace 21 | * Connect to provisioned services 22 | 23 | ![](doc/source/images/architecture.png) 24 | 25 | ## Video 26 | 27 | Learn more about the Backend for Frontend pattern in this tech talk: 28 | 29 | [![BFFs and GraphQL, terms you should know and why](https://img.youtube.com/vi/B5OdK21ZevI/maxresdefault.jpg)](https://www.youtube.com/watch?v=B5OdK21ZevI) 30 | 31 | ## Steps 32 | 33 | > As an alternative to the steps below, you can [create this project as a starter kit](https://cloud.ibm.com/developer/appservice/create-app?starterKit=05f26bad-b3db-3dfb-bb16-2762b0aef2da) on IBM Cloud, which automatically provisions required services, and injects service credentials into a custom fork of this pattern. Then, you can skip directly to step 3 below. 34 | 35 | 1. [Install development tools](#1-install-development-tools) 36 | 1. [Project contents](#2-project-contents) 37 | 1. [Run](#3-run) 38 | 39 | ### 1. Install development tools 40 | 41 | Ensure you have the required development tools installed: 42 | 43 | * [Maven](https://maven.apache.org/install.html) 44 | * Java 8: Any compliant JVM should work. 45 | * [Java 8 JDK from Oracle](https://www.oracle.com/technetwork/java/javase/downloads/index.html) 46 | * [Java 8 JDK from IBM (AIX, Linux, z/OS, IBM i)](https://www.ibm.com/developerworks/java/jdk/) 47 | 48 | ### 2. Project contents 49 | 50 | The BFF application has a health endpoint which is accessible at `:/health`. The ports are set to the defaults of `8080` for HTTP and `8443` for HTTPS and are exposed to the CLI in the `cli-config.yml` file. 51 | 52 | The project contains IBM Cloud specific files that are used to deploy the application as part of a IBM Cloud DevOps flow. The `.bluemix` directory contains files used to define the IBM Cloud toolchain and pipeline for your application. The `manifest.yml` file specifies the name of your application in IBM Cloud, the timeout value during deployment and which services to bind to. 53 | 54 | Credentials are either taken from the `VCAP_SERVICES` environment variable that IBM Cloud provides or from environment variables passed in by the config file `src/main/resources/application-local.properties`. 55 | 56 | ### 3. Run 57 | 58 | To build and run the application: 59 | 60 | 1. `mvn install` 61 | 1. `java -jar ./target/springbackendforfrontend-1.0-SNAPSHOT.jar` 62 | 63 | To run the application in Docker use the Docker file called `Dockerfile`. If you do not want to install Maven locally you can use `Dockerfile-tools` to build a container with Maven installed. 64 | 65 | The application exposes the following endpoints: 66 | 67 | * Health endpoint: `:/health` (for example, `http://localhost:8080/health`). 68 | 69 | The ports are set in the `pom.xml` file and exposed to the CLI in the `cli-config.yml` file. 70 | 71 | ## License 72 | 73 | This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1](https://developercertificate.org/) and the [Apache License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.txt). 74 | 75 | [Apache License FAQ](https://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN) 76 | -------------------------------------------------------------------------------- /doc/source/images/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/spring-backend-for-frontend/b7817f5660837dbaa228122b5d5ba11111164c08/doc/source/images/architecture.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | projects 7 | springbackendforfrontend 8 | 1.0-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | UTF-8 13 | 1.8 14 | 1.8 15 | 1.8 16 | Dalston.SR4 17 | 18 | springbackendforfrontend 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-parent 24 | 1.5.15.RELEASE 25 | 26 | 27 | 28 | 29 | io.springfox 30 | springfox-swagger2 31 | 2.7.0 32 | 33 | 34 | io.springfox 35 | springfox-swagger-ui 36 | 2.7.0 37 | 38 | 39 | com.fasterxml.jackson.datatype 40 | jackson-datatype-joda 41 | 42 | 43 | javax.validation 44 | validation-api 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-web 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-actuator 53 | 54 | 55 | org.springframework.cloud 56 | spring-cloud-starter-hystrix 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-starter-test 61 | test 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.cloud 69 | spring-cloud-dependencies 70 | ${spring-cloud.version} 71 | pom 72 | import 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-maven-plugin 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/main/java/application/ApiException.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 4 | 5 | public class ApiException extends Exception{ 6 | private int code; 7 | public ApiException (int code, String msg) { 8 | super(msg); 9 | this.code = code; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/application/ApiOriginFilter.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.*; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 9 | 10 | public class ApiOriginFilter implements javax.servlet.Filter { 11 | @Override 12 | public void doFilter(ServletRequest request, ServletResponse response, 13 | FilterChain chain) throws IOException, ServletException { 14 | HttpServletResponse res = (HttpServletResponse) response; 15 | res.addHeader("Access-Control-Allow-Origin", "*"); 16 | res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); 17 | res.addHeader("Access-Control-Allow-Headers", "Content-Type"); 18 | chain.doFilter(request, response); 19 | } 20 | 21 | @Override 22 | public void destroy() { 23 | } 24 | 25 | @Override 26 | public void init(FilterConfig filterConfig) throws ServletException { 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/application/ApiResponseMessage.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | import org.springframework.http.HttpStatus; 4 | 5 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 6 | 7 | @javax.xml.bind.annotation.XmlRootElement 8 | public class ApiResponseMessage { 9 | 10 | private HttpStatus code; 11 | private String message; 12 | private T data; 13 | 14 | public ApiResponseMessage(){} 15 | 16 | public ApiResponseMessage(HttpStatus code, String message, T data){ 17 | this.code = code; 18 | this.message = message; 19 | this.data = data; 20 | } 21 | 22 | public ApiResponseMessage(int code, String message, T data){ 23 | this.code = HttpStatus.valueOf(code); 24 | this.message = message; 25 | this.data = data; 26 | } 27 | 28 | public HttpStatus getCode() { 29 | return code; 30 | } 31 | 32 | public void setCode(HttpStatus code) { 33 | this.code = code; 34 | } 35 | 36 | public String getMessage() { 37 | return message; 38 | } 39 | 40 | public void setMessage(String message) { 41 | this.message = message; 42 | } 43 | 44 | public T getData() { 45 | return data; 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/application/NotFoundException.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 4 | 5 | public class NotFoundException extends ApiException { 6 | private int code; 7 | public NotFoundException (int code, String msg) { 8 | super(code, msg); 9 | this.code = code; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/application/ProductApi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * NOTE: This class is auto generated by the swagger code generator program (2.2.3). 3 | * https://github.com/swagger-api/swagger-codegen 4 | * Do not edit the class manually. 5 | */ 6 | package application; 7 | 8 | import application.model.Product; 9 | 10 | import io.swagger.annotations.*; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RequestHeader; 15 | import org.springframework.web.bind.annotation.RequestMapping; 16 | import org.springframework.web.bind.annotation.RequestMethod; 17 | import org.springframework.web.bind.annotation.RequestParam; 18 | import org.springframework.web.bind.annotation.RequestPart; 19 | import org.springframework.web.multipart.MultipartFile; 20 | import java.io.IOException; 21 | 22 | import java.util.List; 23 | import org.springframework.validation.annotation.Validated; 24 | import javax.validation.constraints.*; 25 | import javax.validation.Valid; 26 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 27 | 28 | @Api(value = "product", description = "the product API") 29 | public interface ProductApi { 30 | 31 | @ApiOperation(value = "", notes = "Delete product by ID", response = Product.class, tags={ "products", }) 32 | @ApiResponses(value = { 33 | @ApiResponse(code = 200, message = "One product", response = Product.class) }) 34 | @RequestMapping(value = "/product/{productID}", 35 | produces = { "application/json" }, 36 | method = RequestMethod.DELETE) 37 | ResponseEntity delete(@ApiParam(value = "Product ID",required=true ) @PathVariable("productID") Long productID, @RequestHeader(value = "Accept", required = false) String accept) throws Exception; 38 | 39 | 40 | @ApiOperation(value = "", notes = "Get product by ID", response = Product.class, tags={ "products", }) 41 | @ApiResponses(value = { 42 | @ApiResponse(code = 200, message = "One product", response = Product.class) }) 43 | @RequestMapping(value = "/product/{productID}", 44 | produces = { "application/json" }, 45 | method = RequestMethod.GET) 46 | ResponseEntity get(@ApiParam(value = "Product ID",required=true ) @PathVariable("productID") Long productID, @RequestHeader(value = "Accept", required = false) String accept) throws Exception; 47 | 48 | 49 | @ApiOperation(value = "", notes = "Update product by ID", response = Product.class, tags={ "products", }) 50 | @ApiResponses(value = { 51 | @ApiResponse(code = 200, message = "One product", response = Product.class) }) 52 | @RequestMapping(value = "/product/{productID}", 53 | produces = { "application/json" }, 54 | method = RequestMethod.PUT) 55 | ResponseEntity update(@ApiParam(value = "Product ID",required=true ) @PathVariable("productID") Long productID,@ApiParam(value = "Product name" ,required=true ) @Valid @RequestBody String productName, @RequestHeader(value = "Accept", required = false) String accept) throws Exception; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/application/ProductApiBinding.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | //this is the class that binds to selected services 4 | import application.model.Product; 5 | 6 | import java.util.List; 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | 10 | import javax.annotation.PostConstruct; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.http.HttpStatus; 13 | import org.springframework.stereotype.Component; 14 | import org.springframework.web.multipart.MultipartFile; 15 | 16 | 17 | @Component 18 | public class ProductApiBinding { 19 | 20 | 21 | public ApiResponseMessage delete(Long productID) { 22 | return new ApiResponseMessage<>(HttpStatus.INTERNAL_SERVER_ERROR, "not yet implemented", null); 23 | } 24 | public ApiResponseMessage get(Long productID) { 25 | return new ApiResponseMessage<>(HttpStatus.NOT_FOUND, "", null); 26 | } 27 | public ApiResponseMessage update(Long productID, String productName) { 28 | return new ApiResponseMessage<>(HttpStatus.NOT_FOUND, "", null); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/application/ProductApiController.java: -------------------------------------------------------------------------------- 1 | package application; 2 | //this is the new package generation for spring with service bindings 3 | import application.model.Product; 4 | 5 | import io.swagger.annotations.*; 6 | 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestHeader; 13 | import org.springframework.web.bind.annotation.RequestParam; 14 | import org.springframework.web.bind.annotation.RequestPart; 15 | import org.springframework.web.multipart.MultipartFile; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | 18 | import java.util.List; 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import java.io.IOException; 21 | import javax.validation.constraints.*; 22 | import javax.validation.Valid; 23 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 24 | 25 | @Controller 26 | public class ProductApiController implements ProductApi { 27 | 28 | @Autowired 29 | private ProductApiBinding services; 30 | 31 | private final ObjectMapper objectMapper; 32 | 33 | public ProductApiController(ObjectMapper objectMapper) { 34 | this.objectMapper = objectMapper; 35 | } 36 | 37 | public ResponseEntity delete(@ApiParam(value = "Product ID",required=true ) @PathVariable("productID") Long productID, 38 | @RequestHeader(value = "Accept", required = false) String accept) throws Exception { 39 | 40 | 41 | ApiResponseMessage result = services.delete(productID); 42 | 43 | if (accept != null && accept.contains("application/json")) { 44 | return new ResponseEntity(result.getData(), result.getCode()); 45 | } 46 | 47 | return new ResponseEntity(HttpStatus.BAD_REQUEST); 48 | } 49 | 50 | public ResponseEntity get(@ApiParam(value = "Product ID",required=true ) @PathVariable("productID") Long productID, 51 | @RequestHeader(value = "Accept", required = false) String accept) throws Exception { 52 | 53 | 54 | ApiResponseMessage result = services.get(productID); 55 | 56 | if (accept != null && accept.contains("application/json")) { 57 | return new ResponseEntity(result.getData(), result.getCode()); 58 | } 59 | 60 | return new ResponseEntity(HttpStatus.BAD_REQUEST); 61 | } 62 | 63 | public ResponseEntity update(@ApiParam(value = "Product ID",required=true ) @PathVariable("productID") Long productID, 64 | @ApiParam(value = "Product name" ,required=true ) @Valid @RequestBody String productName, 65 | @RequestHeader(value = "Accept", required = false) String accept) throws Exception { 66 | 67 | 68 | ApiResponseMessage result = services.update(productID, productName); 69 | 70 | if (accept != null && accept.contains("application/json")) { 71 | return new ResponseEntity(result.getData(), result.getCode()); 72 | } 73 | 74 | return new ResponseEntity(HttpStatus.BAD_REQUEST); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/application/ProductsApi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * NOTE: This class is auto generated by the swagger code generator program (2.2.3). 3 | * https://github.com/swagger-api/swagger-codegen 4 | * Do not edit the class manually. 5 | */ 6 | package application; 7 | 8 | import application.model.Product; 9 | 10 | import io.swagger.annotations.*; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RequestHeader; 15 | import org.springframework.web.bind.annotation.RequestMapping; 16 | import org.springframework.web.bind.annotation.RequestMethod; 17 | import org.springframework.web.bind.annotation.RequestParam; 18 | import org.springframework.web.bind.annotation.RequestPart; 19 | import org.springframework.web.multipart.MultipartFile; 20 | import java.io.IOException; 21 | 22 | import java.util.List; 23 | import org.springframework.validation.annotation.Validated; 24 | import javax.validation.constraints.*; 25 | import javax.validation.Valid; 26 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 27 | 28 | @Api(value = "products", description = "the products API") 29 | public interface ProductsApi { 30 | 31 | @ApiOperation(value = "", notes = "Add new product", response = Product.class, tags={ "products", }) 32 | @ApiResponses(value = { 33 | @ApiResponse(code = 200, message = "One product", response = Product.class) }) 34 | @RequestMapping(value = "/products", 35 | produces = { "application/json" }, 36 | method = RequestMethod.POST) 37 | ResponseEntity add(@ApiParam(value = "Product name" ,required=true ) @Valid @RequestBody String productName, @RequestHeader(value = "Accept", required = false) String accept) throws Exception; 38 | 39 | 40 | @ApiOperation(value = "", notes = "Get all products", response = Product.class, responseContainer = "List", tags={ "products", }) 41 | @ApiResponses(value = { 42 | @ApiResponse(code = 200, message = "List of all products", response = Product.class, responseContainer = "List") }) 43 | @RequestMapping(value = "/products", 44 | produces = { "application/json" }, 45 | method = RequestMethod.GET) 46 | ResponseEntity> getAll( @RequestHeader(value = "Accept", required = false) String accept) throws Exception; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/application/ProductsApiBinding.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | //this is the class that binds to selected services 4 | import application.model.Product; 5 | 6 | import java.util.List; 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | 10 | import javax.annotation.PostConstruct; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.http.HttpStatus; 13 | import org.springframework.stereotype.Component; 14 | import org.springframework.web.multipart.MultipartFile; 15 | 16 | 17 | @Component 18 | public class ProductsApiBinding { 19 | 20 | 21 | public ApiResponseMessage add(String productName) { 22 | return new ApiResponseMessage<>(HttpStatus.NOT_FOUND, "", null); 23 | } 24 | public ApiResponseMessage> getAll() { 25 | return new ApiResponseMessage<>(HttpStatus.NOT_FOUND, "", null); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/application/ProductsApiController.java: -------------------------------------------------------------------------------- 1 | package application; 2 | //this is the new package generation for spring with service bindings 3 | import application.model.Product; 4 | 5 | import io.swagger.annotations.*; 6 | 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestHeader; 13 | import org.springframework.web.bind.annotation.RequestParam; 14 | import org.springframework.web.bind.annotation.RequestPart; 15 | import org.springframework.web.multipart.MultipartFile; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | 18 | import java.util.List; 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import java.io.IOException; 21 | import javax.validation.constraints.*; 22 | import javax.validation.Valid; 23 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 24 | 25 | @Controller 26 | public class ProductsApiController implements ProductsApi { 27 | 28 | @Autowired 29 | private ProductsApiBinding services; 30 | 31 | private final ObjectMapper objectMapper; 32 | 33 | public ProductsApiController(ObjectMapper objectMapper) { 34 | this.objectMapper = objectMapper; 35 | } 36 | 37 | public ResponseEntity add(@ApiParam(value = "Product name" ,required=true ) @Valid @RequestBody String productName, 38 | @RequestHeader(value = "Accept", required = false) String accept) throws Exception { 39 | 40 | 41 | ApiResponseMessage result = services.add(productName); 42 | 43 | if (accept != null && accept.contains("application/json")) { 44 | return new ResponseEntity(result.getData(), result.getCode()); 45 | } 46 | 47 | return new ResponseEntity(HttpStatus.BAD_REQUEST); 48 | } 49 | 50 | public ResponseEntity> getAll(@RequestHeader(value = "Accept", required = false) String accept) throws Exception { 51 | 52 | 53 | ApiResponseMessage> result = services.getAll(); 54 | 55 | if (accept != null && accept.contains("application/json")) { 56 | return new ResponseEntity>(result.getData(), result.getCode()); 57 | } 58 | 59 | return new ResponseEntity>(HttpStatus.BAD_REQUEST); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/application/SBApplication.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.ComponentScan; 6 | 7 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 8 | 9 | @SpringBootApplication 10 | @EnableSwagger2 11 | @ComponentScan(basePackages = { "io.swagger", "application" }) 12 | public class SBApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(SBApplication.class, args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/application/model/Product.java: -------------------------------------------------------------------------------- 1 | package application.model; 2 | 3 | import java.util.Objects; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import com.fasterxml.jackson.annotation.JsonCreator; 6 | import io.swagger.annotations.ApiModel; 7 | import io.swagger.annotations.ApiModelProperty; 8 | import org.springframework.validation.annotation.Validated; 9 | import javax.validation.Valid; 10 | import javax.validation.constraints.*; 11 | 12 | /** 13 | * A product object 14 | */ 15 | @ApiModel(description = "A product object") 16 | @Validated 17 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 18 | 19 | public class Product { 20 | @JsonProperty("identifier") 21 | private Long identifier = null; 22 | 23 | @JsonProperty("name") 24 | private String name = null; 25 | 26 | public Product identifier(Long identifier) { 27 | this.identifier = identifier; 28 | return this; 29 | } 30 | 31 | /** 32 | * Get identifier 33 | * @return identifier 34 | **/ 35 | @ApiModelProperty(required = true, value = "") 36 | @NotNull 37 | 38 | 39 | public Long getIdentifier() { 40 | return identifier; 41 | } 42 | 43 | public void setIdentifier(Long identifier) { 44 | this.identifier = identifier; 45 | } 46 | 47 | public Product name(String name) { 48 | this.name = name; 49 | return this; 50 | } 51 | 52 | /** 53 | * Get name 54 | * @return name 55 | **/ 56 | @ApiModelProperty(required = true, value = "") 57 | @NotNull 58 | 59 | 60 | public String getName() { 61 | return name; 62 | } 63 | 64 | public void setName(String name) { 65 | this.name = name; 66 | } 67 | 68 | 69 | @Override 70 | public boolean equals(java.lang.Object o) { 71 | if (this == o) { 72 | return true; 73 | } 74 | if (o == null || getClass() != o.getClass()) { 75 | return false; 76 | } 77 | Product product = (Product) o; 78 | return Objects.equals(this.identifier, product.identifier) && 79 | Objects.equals(this.name, product.name); 80 | } 81 | 82 | @Override 83 | public int hashCode() { 84 | return Objects.hash(identifier, name); 85 | } 86 | 87 | @Override 88 | public String toString() { 89 | StringBuilder sb = new StringBuilder(); 90 | sb.append("class Product {\n"); 91 | 92 | sb.append(" identifier: ").append(toIndentedString(identifier)).append("\n"); 93 | sb.append(" name: ").append(toIndentedString(name)).append("\n"); 94 | sb.append("}"); 95 | return sb.toString(); 96 | } 97 | 98 | /** 99 | * Convert the given object to string with each line indented by 4 spaces 100 | * (except the first line). 101 | */ 102 | private String toIndentedString(java.lang.Object o) { 103 | if (o == null) { 104 | return "null"; 105 | } 106 | return o.toString().replace("\n", "\n "); 107 | } 108 | } 109 | 110 | -------------------------------------------------------------------------------- /src/main/java/application/rest/HealthEndpoint.java: -------------------------------------------------------------------------------- 1 | package application.rest; 2 | 3 | import org.springframework.boot.actuate.health.Health; 4 | import org.springframework.boot.actuate.health.HealthIndicator; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class HealthEndpoint implements HealthIndicator { 9 | 10 | @Override 11 | public Health health() { 12 | /* 13 | if (!healthy) { 14 | return Health.down().withDetail("Not healthy", 500).build(); 15 | } 16 | */ 17 | return Health.up().build(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/io/swagger/Info.java: -------------------------------------------------------------------------------- 1 | package io.swagger; 2 | 3 | import org.springframework.context.event.EventListener; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.boot.context.event.ApplicationReadyEvent; 6 | 7 | @Component 8 | public class Info { 9 | 10 | @EventListener(ApplicationReadyEvent.class) 11 | public void contextRefreshedEvent() { 12 | System.out.println("The following endpoints are available by default :-"); 13 | System.out.println(" OpenAPI UI : http://localhost:8080/swagger-ui.html"); 14 | System.out.println(" OpenAPI json : http://localhost:8080/swagger/api"); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/main/java/io/swagger/RFC3339DateFormat.java: -------------------------------------------------------------------------------- 1 | package io.swagger; 2 | 3 | import com.fasterxml.jackson.databind.util.ISO8601DateFormat; 4 | import com.fasterxml.jackson.databind.util.ISO8601Utils; 5 | 6 | import java.text.FieldPosition; 7 | import java.util.Date; 8 | 9 | 10 | public class RFC3339DateFormat extends ISO8601DateFormat { 11 | 12 | // Same as ISO8601DateFormat but serializing milliseconds. 13 | @Override 14 | public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) { 15 | String value = ISO8601Utils.format(date, true); 16 | toAppendTo.append(value); 17 | return toAppendTo; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/io/swagger/configuration/SwaggerDocumentationConfig.java: -------------------------------------------------------------------------------- 1 | package io.swagger.configuration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | import springfox.documentation.builders.ApiInfoBuilder; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.service.ApiInfo; 9 | import springfox.documentation.service.Contact; 10 | import springfox.documentation.spi.DocumentationType; 11 | import springfox.documentation.spring.web.plugins.Docket; 12 | 13 | @javax.annotation.Generated(value = "com.ibm.mobile.sdkgen.platform.JavaSpringBindingsConfig", date = "2019-03-26T21:21:29.320Z") 14 | 15 | @Configuration 16 | public class SwaggerDocumentationConfig { 17 | 18 | ApiInfo apiInfo() { 19 | return new ApiInfoBuilder() 20 | .title("Products API") 21 | .description("No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)") 22 | .license("") 23 | .licenseUrl("http://unlicense.org") 24 | .termsOfServiceUrl("") 25 | .version("0.0.2") 26 | .contact(new Contact("","", "")) 27 | .build(); 28 | } 29 | 30 | @Bean 31 | public Docket customImplementation(){ 32 | return new Docket(DocumentationType.SWAGGER_2) 33 | .select() 34 | .apis(RequestHandlerSelectors.basePackage("application")) 35 | .build() 36 | .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class) 37 | .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class) 38 | .apiInfo(apiInfo()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | springfox.documentation.swagger.v2.path=/swagger/api 2 | spring.jackson.date-format=io.swagger.RFC3339DateFormat 3 | spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false 4 | -------------------------------------------------------------------------------- /src/main/resources/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IBM Cloud Starter 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 |

Congratulations!

14 | 15 |

You are currently running a Spring server built for the IBM Cloud.

16 |
17 |
18 | 63 |
64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/test/java/application/HealthEndpointTest.java: -------------------------------------------------------------------------------- 1 | package application; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.context.embedded.LocalServerPort; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 12 | import org.springframework.boot.test.web.client.TestRestTemplate; 13 | import org.springframework.test.context.junit4.SpringRunner; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 17 | public class HealthEndpointTest { 18 | 19 | @Autowired 20 | private TestRestTemplate server; 21 | 22 | @LocalServerPort 23 | private int port; 24 | 25 | @Test 26 | public void testEndpoint() throws Exception { 27 | String endpoint = "http://localhost:" + port + "/health"; 28 | String response = server.getForObject(endpoint, String.class); 29 | assertTrue("Invalid response from server : " + response, response.startsWith("{\"status\":\"UP\"")); 30 | } 31 | 32 | } 33 | --------------------------------------------------------------------------------