From 2f88dfa82e512f291bd4790b55266a808aaed802 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Wed, 25 Apr 2018 12:19:35 +0200 Subject: [PATCH 1/6] adapted Jersey minimal sample for CXF --- java/java-cxf-spring-boot-minimal/README.md | 22 +++ java/java-cxf-spring-boot-minimal/pom.xml | 78 ++++++++++ .../java/io/swagger/sample/MyApplication.java | 16 ++ .../java/io/swagger/sample/data/PetData.java | 145 ++++++++++++++++++ .../sample/exception/ApiException.java | 25 +++ .../sample/exception/BadRequestException.java | 25 +++ .../sample/exception/NotFoundException.java | 25 +++ .../io/swagger/sample/model/ApiResponse.java | 84 ++++++++++ .../io/swagger/sample/model/Category.java | 43 ++++++ .../java/io/swagger/sample/model/Pet.java | 101 ++++++++++++ .../java/io/swagger/sample/model/Tag.java | 43 ++++++ .../swagger/sample/resource/PetResource.java | 97 ++++++++++++ .../sample/resource/QueryResultBean.java | 42 +++++ .../resource/SampleExceptionMapper.java | 61 ++++++++ .../src/main/resources/application.yml | 4 + pom.xml | 6 +- 16 files changed, 814 insertions(+), 3 deletions(-) create mode 100644 java/java-cxf-spring-boot-minimal/README.md create mode 100644 java/java-cxf-spring-boot-minimal/pom.xml create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/data/PetData.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/ApiException.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/BadRequestException.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/NotFoundException.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/ApiResponse.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Category.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Pet.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Tag.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/QueryResultBean.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/SampleExceptionMapper.java create mode 100644 java/java-cxf-spring-boot-minimal/src/main/resources/application.yml diff --git a/java/java-cxf-spring-boot-minimal/README.md b/java/java-cxf-spring-boot-minimal/README.md new file mode 100644 index 00000000..0b40f7eb --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/README.md @@ -0,0 +1,22 @@ +# Swagger Sample App + +## Overview +This is a java project to build a stand-alone server which implements the OpenAPI Spec. You can find out +more about both the spec and the framework at http://swagger.io. + +This sample is based on CXF and Spring Boot, and provides a minimal example of integration of swagger into a CXF based app. + +### To run (with Maven) +To run the server, run this task: + +``` +mvn spring-boot:run +``` + +This will start Tomcat embedded on port 8080. + +### Testing the server +Once started, you can navigate to http://localhost:8080/services/openapi.json to view the Swagger Resource Listing. +This tells you that the server is up and ready to demonstrate Swagger. + +Swagger-UI is reachable at: http://localhost:8080/services/api-docs?url=/services/openapi.yaml \ No newline at end of file diff --git a/java/java-cxf-spring-boot-minimal/pom.xml b/java/java-cxf-spring-boot-minimal/pom.xml new file mode 100644 index 00000000..725fbd52 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/pom.xml @@ -0,0 +1,78 @@ + + + io.swagger.samples.v3 + swagger-samples-project + 2.0.0 + ../.. + + 4.0.0 + io.swagger.samples.v3 + swagger-cxf-spring-boot-minimal-sample-app + jar + swagger-cxf-spring-boot-minimal-sample-app + 2.0.0 + + 3.2.4 + 2.0.1.RELEASE + + + src/main/java + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + repackage + + + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + io.swagger.core.v3 + swagger-jaxrs2 + compile + ${swagger-version} + + + org.apache.cxf + cxf-spring-boot-starter-jaxrs + ${cxf.version} + + + org.webjars + swagger-ui + 3.13.6 + + + org.apache.cxf + cxf-rt-rs-service-description-openapi-v3 + ${cxf.version} + + + diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java new file mode 100644 index 00000000..6bd28e41 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java @@ -0,0 +1,16 @@ +package io.swagger.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@SpringBootApplication +public class MyApplication { + + public static void main(String[] args) { + SpringApplication.run(MyApplication.class, args); + } + +} \ No newline at end of file diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/data/PetData.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/data/PetData.java new file mode 100644 index 00000000..21f8f317 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/data/PetData.java @@ -0,0 +1,145 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.data; + +import io.swagger.sample.model.Category; +import io.swagger.sample.model.Pet; +import io.swagger.sample.model.Tag; + +import java.util.List; +import java.util.ArrayList; + +public class PetData { + static List pets = new ArrayList(); + static List categories = new ArrayList(); + + static { + categories.add(createCategory(1, "Dogs")); + categories.add(createCategory(2, "Cats")); + categories.add(createCategory(3, "Rabbits")); + categories.add(createCategory(4, "Lions")); + + pets.add(createPet(1, categories.get(1), "Cat 1", new String[] { + "url1", "url2" }, new String[] { "tag1", "tag2" }, "available")); + pets.add(createPet(2, categories.get(1), "Cat 2", new String[] { + "url1", "url2" }, new String[] { "tag2", "tag3" }, "available")); + pets.add(createPet(3, categories.get(1), "Cat 3", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "pending")); + + pets.add(createPet(4, categories.get(0), "Dog 1", new String[] { + "url1", "url2" }, new String[] { "tag1", "tag2" }, "available")); + pets.add(createPet(5, categories.get(0), "Dog 2", new String[] { + "url1", "url2" }, new String[] { "tag2", "tag3" }, "sold")); + pets.add(createPet(6, categories.get(0), "Dog 3", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "pending")); + + pets.add(createPet(7, categories.get(3), "Lion 1", new String[] { + "url1", "url2" }, new String[] { "tag1", "tag2" }, "available")); + pets.add(createPet(8, categories.get(3), "Lion 2", new String[] { + "url1", "url2" }, new String[] { "tag2", "tag3" }, "available")); + pets.add(createPet(9, categories.get(3), "Lion 3", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "available")); + + pets.add(createPet(10, categories.get(2), "Rabbit 1", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "available")); + } + + public Pet getPetById(long petId) { + for (Pet pet : pets) { + if (pet.getId() == petId) { + return pet; + } + } + return null; + } + + public List findPetByStatus(String status) { + String[] statues = status.split(","); + List result = new java.util.ArrayList(); + for (Pet pet : pets) { + for (String s : statues) { + if (s.equals(pet.getStatus())) { + result.add(pet); + } + } + } + return result; + } + + public List findPetByTags(String tags) { + String[] tagList = tags.split(","); + List result = new java.util.ArrayList(); + for (Pet pet : pets) { + if (null != pet.getTags()) { + for (Tag tag : pet.getTags()) { + for (String tagListString : tagList) { + if (tagListString.equals(tag.getName())) + result.add(pet); + } + } + } + } + return result; + } + + public void addPet(Pet pet) { + if (pets.size() > 0) { + for (int i = pets.size() - 1; i >= 0; i--) { + if (pets.get(i).getId() == pet.getId()) { + pets.remove(i); + } + } + } + pets.add(pet); + } + + static Pet createPet(long id, Category cat, String name, String[] urls, + String[] tags, String status) { + Pet pet = new Pet(); + pet.setId(id); + pet.setCategory(cat); + pet.setName(name); + if (null != urls) { + List urlObjs = new ArrayList(); + for (String urlString : urls) { + urlObjs.add(urlString); + } + pet.setPhotoUrls(urlObjs); + } + List tagObjs = new java.util.ArrayList(); + int i = 0; + if (null != tags) { + for (String tagString : tags) { + i = i + 1; + Tag tag = new Tag(); + tag.setId(i); + tag.setName(tagString); + tagObjs.add(tag); + } + } + pet.setTags(tagObjs); + pet.setStatus(status); + return pet; + } + + static Category createCategory(long id, String name) { + Category category = new Category(); + category.setId(id); + category.setName(name); + return category; + } +} \ No newline at end of file diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/ApiException.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/ApiException.java new file mode 100644 index 00000000..2c01fb94 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/ApiException.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.exception; + +public class ApiException extends Exception{ + private int code; + public ApiException (int code, String msg) { + super(msg); + this.code = code; + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/BadRequestException.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/BadRequestException.java new file mode 100644 index 00000000..841fb781 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/BadRequestException.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.exception; + +public class BadRequestException extends ApiException{ + private int code; + public BadRequestException (int code, String msg) { + super(code, msg); + this.code = code; + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/NotFoundException.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/NotFoundException.java new file mode 100644 index 00000000..b3b766c6 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/exception/NotFoundException.java @@ -0,0 +1,25 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.exception; + +public class NotFoundException extends ApiException { + private int code; + public NotFoundException (int code, String msg) { + super(code, msg); + this.code = code; + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/ApiResponse.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/ApiResponse.java new file mode 100644 index 00000000..e6ce4e70 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/ApiResponse.java @@ -0,0 +1,84 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.model; + +import javax.xml.bind.annotation.XmlTransient; + +@javax.xml.bind.annotation.XmlRootElement +public class ApiResponse { + public static final int ERROR = 1; + public static final int WARNING = 2; + public static final int INFO = 3; + public static final int OK = 4; + public static final int TOO_BUSY = 5; + + int code; + String type; + String message; + + public ApiResponse(){} + + public ApiResponse(int code, String message){ + this.code = code; + switch(code){ + case ERROR: + setType("error"); + break; + case WARNING: + setType("warning"); + break; + case INFO: + setType("info"); + break; + case OK: + setType("ok"); + break; + case TOO_BUSY: + setType("too busy"); + break; + default: + setType("unknown"); + break; + } + this.message = message; + } + + @XmlTransient + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Category.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Category.java new file mode 100644 index 00000000..d43e0bed --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Category.java @@ -0,0 +1,43 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.model; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "Category") +public class Category { + private long id; + private String name; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Pet.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Pet.java new file mode 100644 index 00000000..3db69e10 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Pet.java @@ -0,0 +1,101 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.model; + +import io.swagger.v3.oas.annotations.media.Schema; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.ArrayList; +import java.util.List; + +@XmlRootElement(name = "Pet") +public class Pet { + private long id; + private Category category; + private String name; + private List photoUrls = new ArrayList(); + private List tags = new ArrayList(); + private String status; + + private int[] lorem; + + public int[] getLorem() { + return lorem; + } + + public void setLorem(int[] lorem) { + this.lorem = lorem; + } + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "category") + public Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } + + @XmlElement(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlElementWrapper(name = "photoUrls") + @XmlElement(name = "photoUrl") + public List getPhotoUrls() { + return photoUrls; + } + + public void setPhotoUrls(List photoUrls) { + this.photoUrls = photoUrls; + } + + @XmlElementWrapper(name = "tags") + @XmlElement(name = "tag") + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + @XmlElement(name = "status") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Tag.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Tag.java new file mode 100644 index 00000000..1677270e --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/model/Tag.java @@ -0,0 +1,43 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.model; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "Tag") +public class Tag { + private long id; + private String name; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java new file mode 100644 index 00000000..e2714cbf --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java @@ -0,0 +1,97 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.resource; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.sample.data.PetData; +import io.swagger.sample.model.Pet; +import org.springframework.stereotype.Service; + +import javax.validation.constraints.NotNull; +import javax.ws.rs.BeanParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.Consumes; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import java.util.List; + +@Service +@Path("/pet") +@Produces({"application/json", "application/xml"}) +public class PetResource { + static PetData petData = new PetData(); + + @GET + @Path("/{petId}") + public Pet getPetById(@PathParam("petId") Long petId) throws io.swagger.sample.exception.NotFoundException { + Pet pet = petData.getPetById(petId); + if (null != pet) { + return pet; + } else { + throw new io.swagger.sample.exception.NotFoundException(404, "Pet not found"); + } + } + + @POST + @Consumes("application/json") + public Response addPet( + @Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) { + petData.addPet(pet); + return Response.ok().entity("SUCCESS").build(); + } + + @PUT + public Response updatePet( + @Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) { + petData.addPet(pet); + return Response.ok().entity("SUCCESS").build(); + } + + @GET + @Path("/findByStatus") + public List findPetsByStatus( + @Parameter( + description = "Status values that need to be considered for filter", + required = true, + schema = @Schema( + allowableValues = {"available","pending","sold"}, + defaultValue = "available" + ) + ) + @QueryParam("status") String status, + @BeanParam QueryResultBean qr +){ + return petData.findPetByStatus(status); + } + + @GET + @Path("/findByTags") + @Deprecated + public List findPetsByTags( + @NotNull @QueryParam("tags") String tags) { + return petData.findPetByTags(tags); + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/QueryResultBean.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/QueryResultBean.java new file mode 100644 index 00000000..f2a2539b --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/QueryResultBean.java @@ -0,0 +1,42 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.resource; + +import javax.ws.rs.*; + +public class QueryResultBean { + @QueryParam("skip") + private Integer skip; + + @QueryParam("limit") + private Integer limit; + + + public Integer getSkip() { + return skip; + } + public void setSkip(Integer skip) { + this.skip = skip; + } + + public Integer getLimit() { + return limit; + } + public void setLimit(Integer limit) { + this.limit = limit; + } +} diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/SampleExceptionMapper.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/SampleExceptionMapper.java new file mode 100644 index 00000000..beabee4b --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/SampleExceptionMapper.java @@ -0,0 +1,61 @@ +/** + * Copyright 2016 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.swagger.sample.resource; + +import io.swagger.sample.exception.ApiException; +import io.swagger.sample.exception.BadRequestException; +import io.swagger.sample.exception.NotFoundException; +import io.swagger.sample.model.ApiResponse; + +import javax.ws.rs.ext.*; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +@Provider +public class SampleExceptionMapper implements ExceptionMapper { + public Response toResponse(Exception exception) { + if (exception instanceof javax.ws.rs.WebApplicationException) { + javax.ws.rs.WebApplicationException e = (javax.ws.rs.WebApplicationException) exception; + return Response + .status(e.getResponse().getStatus()) + .entity(new ApiResponse(e.getResponse().getStatus(), + exception.getMessage())).build(); + } else if (exception instanceof com.fasterxml.jackson.core.JsonParseException) { + return Response.status(400) + .entity(new ApiResponse(400, "bad input")).build(); + } else if (exception instanceof NotFoundException) { + return Response + .status(Status.NOT_FOUND) + .entity(new ApiResponse(ApiResponse.ERROR, exception + .getMessage())).build(); + } else if (exception instanceof BadRequestException) { + return Response + .status(Status.BAD_REQUEST) + .entity(new ApiResponse(ApiResponse.ERROR, exception + .getMessage())).build(); + } else if (exception instanceof ApiException) { + return Response + .status(Status.BAD_REQUEST) + .entity(new ApiResponse(ApiResponse.ERROR, exception + .getMessage())).build(); + } else { + return Response.status(500) + .entity(new ApiResponse(500, "something bad happened")) + .build(); + } + } +} \ No newline at end of file diff --git a/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml b/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml new file mode 100644 index 00000000..e3f88819 --- /dev/null +++ b/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml @@ -0,0 +1,4 @@ +cxf: + jaxrs: + component-scan: true + classes-scan-packages: org.apache.cxf.jaxrs.openapi,org.apache.cxf.metrics diff --git a/pom.xml b/pom.xml index 0f301346..c6ba3dc1 100644 --- a/pom.xml +++ b/pom.xml @@ -282,6 +282,7 @@ + java/java-cxf-spring-boot-minimal java/java-dropwizard java/java-jaxrs2-resources java/java-jaxrs2-openapiservlet @@ -449,19 +450,18 @@ 1.2 2.2 2.0.1 - 2.1.4 2.10.4 2.3.4 3.1.0 2.26 9.4.9.v20180320 - 2.9.1 + 2.9.5 1.0.1 4.8.1 1.0.0 3.2.1 - 1.6.3 + 1.7.25 2.1.3 3.1.5 0.90 From 49b2640f4e90cd3e2333cc21fa602bca09b8e6f1 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Wed, 25 Apr 2018 14:41:09 +0200 Subject: [PATCH 2/6] merged more detailled annotations from swagger-jaxrs2-resources --- .../swagger/sample/resource/PetResource.java | 85 +++++++++++++++---- 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java index e2714cbf..ad9994f7 100644 --- a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java @@ -25,7 +25,6 @@ import io.swagger.sample.model.Pet; import org.springframework.stereotype.Service; -import javax.validation.constraints.NotNull; import javax.ws.rs.BeanParam; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -46,7 +45,27 @@ public class PetResource { @GET @Path("/{petId}") - public Pet getPetById(@PathParam("petId") Long petId) throws io.swagger.sample.exception.NotFoundException { + @Operation(summary = "Find pet by ID", + tags = {"pets"}, + description = "Returns a pet when 0 < ID <= 10. ID > 10 or nonintegers will simulate API error conditions", + responses = { + @ApiResponse(description = "The pet", content = @Content( + schema = @Schema(implementation = Pet.class) + )), + @ApiResponse(responseCode = "400", description = "Invalid ID supplied"), + @ApiResponse(responseCode = "404", description = "Pet not found") + }) + public Pet getPetById( + @Parameter( + description = "ID of pet that needs to be fetched", + schema = @Schema( + type = "integer", + format = "int64", + description = "param ID of pet that needs to be fetched", + allowableValues = {"1","2","3"} + ), + required = true) + @PathParam("petId") Long petId) throws io.swagger.sample.exception.NotFoundException { Pet pet = petData.getPetById(petId); if (null != pet) { return pet; @@ -57,41 +76,73 @@ public Pet getPetById(@PathParam("petId") Long petId) throws io.swagger.sample.e @POST @Consumes("application/json") + @Operation(summary = "Add a new pet to the store", + tags = {"pets"}, + responses = { + @ApiResponse(responseCode = "405", description = "Invalid input") + }) public Response addPet( - @Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) { + @Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) { petData.addPet(pet); return Response.ok().entity("SUCCESS").build(); } @PUT + @Operation(summary = "Update an existing pet", + tags = {"pets"}, + responses = { + @ApiResponse(responseCode = "400", description = "Invalid ID supplied"), + @ApiResponse(responseCode = "404", description = "Pet not found"), + @ApiResponse(responseCode = "405", description = "Validation exception") }) public Response updatePet( - @Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) { + @Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) { petData.addPet(pet); return Response.ok().entity("SUCCESS").build(); } @GET @Path("/findByStatus") + @Operation(summary = "Finds Pets by status", + tags = {"pets"}, + description = "Multiple status values can be provided with comma seperated strings", + responses = { + @ApiResponse( + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = Pet.class))), + @ApiResponse( + responseCode = "400", description = "Invalid status value" + )} + ) public List findPetsByStatus( - @Parameter( - description = "Status values that need to be considered for filter", - required = true, - schema = @Schema( - allowableValues = {"available","pending","sold"}, - defaultValue = "available" - ) - ) - @QueryParam("status") String status, - @BeanParam QueryResultBean qr -){ + @Parameter( + description = "Status values that need to be considered for filter", + required = true, + schema = @Schema( + allowableValues = {"available","pending","sold"}, + defaultValue = "available" + ) + ) + @QueryParam("status") String status, + @BeanParam QueryResultBean qr + ){ return petData.findPetByStatus(status); } @GET @Path("/findByTags") + @Operation(summary = "Finds Pets by tags", + tags = {"pets"}, + description = "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", + responses = { + @ApiResponse(description = "Pets matching criteria", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = Pet.class)) + ), + @ApiResponse(description = "Invalid tag value", responseCode = "400") + }) @Deprecated public List findPetsByTags( - @NotNull @QueryParam("tags") String tags) { + @Parameter(description = "Tags to filter by", required = true) @QueryParam("tags") String tags) { return petData.findPetByTags(tags); } -} +} \ No newline at end of file From afd09992dc2b343254cc51d0db3d973b6482044c Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Wed, 25 Apr 2018 15:38:03 +0200 Subject: [PATCH 3/6] fix JSON parsing, add meta info --- java/java-cxf-spring-boot-minimal/pom.xml | 4 ++++ .../java/io/swagger/sample/MyApplication.java | 3 --- .../io/swagger/sample/resource/PetResource.java | 15 +++++++++++---- .../src/main/resources/application.yml | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/java/java-cxf-spring-boot-minimal/pom.xml b/java/java-cxf-spring-boot-minimal/pom.xml index 725fbd52..8bb4062b 100644 --- a/java/java-cxf-spring-boot-minimal/pom.xml +++ b/java/java-cxf-spring-boot-minimal/pom.xml @@ -74,5 +74,9 @@ cxf-rt-rs-service-description-openapi-v3 ${cxf.version} + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java index 6bd28e41..241a093d 100644 --- a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/MyApplication.java @@ -3,9 +3,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import javax.ws.rs.ApplicationPath; -import javax.ws.rs.core.Application; - @SpringBootApplication public class MyApplication { diff --git a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java index ad9994f7..2b623fdd 100644 --- a/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java +++ b/java/java-cxf-spring-boot-minimal/src/main/java/io/swagger/sample/resource/PetResource.java @@ -16,13 +16,16 @@ package io.swagger.sample.resource; +import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.info.Info; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.sample.data.PetData; import io.swagger.sample.model.Pet; +import io.swagger.v3.oas.annotations.servers.Server; import org.springframework.stereotype.Service; import javax.ws.rs.BeanParam; @@ -34,12 +37,15 @@ import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @Service +@OpenAPIDefinition( + info = @Info(title = "Petstore", version = "2.0.0", description = "This is a sample Petstore server.")) @Path("/pet") -@Produces({"application/json", "application/xml"}) +@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public class PetResource { static PetData petData = new PetData(); @@ -75,7 +81,7 @@ public Pet getPetById( } @POST - @Consumes("application/json") + @Consumes(MediaType.APPLICATION_JSON) @Operation(summary = "Add a new pet to the store", tags = {"pets"}, responses = { @@ -88,6 +94,7 @@ public Response addPet( } @PUT + @Consumes(MediaType.APPLICATION_JSON) @Operation(summary = "Update an existing pet", tags = {"pets"}, responses = { @@ -107,7 +114,7 @@ public Response updatePet( description = "Multiple status values can be provided with comma seperated strings", responses = { @ApiResponse( - content = @Content(mediaType = "application/json", + content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Pet.class))), @ApiResponse( responseCode = "400", description = "Invalid status value" @@ -135,7 +142,7 @@ public List findPetsByStatus( description = "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", responses = { @ApiResponse(description = "Pets matching criteria", - content = @Content(mediaType = "application/json", + content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Pet.class)) ), @ApiResponse(description = "Invalid tag value", responseCode = "400") diff --git a/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml b/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml index e3f88819..607cef7e 100644 --- a/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml +++ b/java/java-cxf-spring-boot-minimal/src/main/resources/application.yml @@ -1,4 +1,4 @@ cxf: jaxrs: component-scan: true - classes-scan-packages: org.apache.cxf.jaxrs.openapi,org.apache.cxf.metrics + classes-scan-packages: org.apache.cxf.jaxrs.openapi,org.apache.cxf.metrics,com.fasterxml.jackson.jaxrs.json From 7f90a41683c0530d7e6a2818ddb3bd2118ed0251 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Fri, 8 Jun 2018 09:40:07 +0200 Subject: [PATCH 4/6] update Spring Boot version --- java/java-cxf-spring-boot-minimal/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/java-cxf-spring-boot-minimal/pom.xml b/java/java-cxf-spring-boot-minimal/pom.xml index 8bb4062b..74550985 100644 --- a/java/java-cxf-spring-boot-minimal/pom.xml +++ b/java/java-cxf-spring-boot-minimal/pom.xml @@ -13,7 +13,7 @@ 2.0.0 3.2.4 - 2.0.1.RELEASE + 2.0.2.RELEASE src/main/java From 5b0d576196413c72e448954351f6ee4757f0ea98 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Fri, 8 Jun 2018 10:01:17 +0200 Subject: [PATCH 5/6] update swagger ui version --- java/java-cxf-spring-boot-minimal/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/java-cxf-spring-boot-minimal/pom.xml b/java/java-cxf-spring-boot-minimal/pom.xml index 74550985..c5c2dc08 100644 --- a/java/java-cxf-spring-boot-minimal/pom.xml +++ b/java/java-cxf-spring-boot-minimal/pom.xml @@ -67,7 +67,7 @@ org.webjars swagger-ui - 3.13.6 + 3.17.0 org.apache.cxf From 7da04d1955177fbb39a1fbfde6718f9e88572c68 Mon Sep 17 00:00:00 2001 From: Dennis Kieselhorst Date: Fri, 28 Sep 2018 15:25:27 +0200 Subject: [PATCH 6/6] update to latest CXF, Spring Boot and Swagger UI version --- java/java-cxf-spring-boot-minimal/pom.xml | 26 +++++++++++------------ pom.xml | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/java/java-cxf-spring-boot-minimal/pom.xml b/java/java-cxf-spring-boot-minimal/pom.xml index c5c2dc08..06ac9131 100644 --- a/java/java-cxf-spring-boot-minimal/pom.xml +++ b/java/java-cxf-spring-boot-minimal/pom.xml @@ -12,8 +12,8 @@ swagger-cxf-spring-boot-minimal-sample-app 2.0.0 - 3.2.4 - 2.0.2.RELEASE + 3.3.0-SNAPSHOT + 2.1.0.RELEASE src/main/java @@ -41,16 +41,16 @@ - - - - org.springframework.boot - spring-boot-dependencies - ${spring-boot.version} - pom - import - - + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + @@ -67,7 +67,7 @@ org.webjars swagger-ui - 3.17.0 + 3.19.5 org.apache.cxf diff --git a/pom.xml b/pom.xml index c6ba3dc1..23e84964 100644 --- a/pom.xml +++ b/pom.xml @@ -449,7 +449,7 @@ 1.2 2.2 - 2.0.1 + 2.0.5 2.10.4 2.3.4 3.1.0