From 674d3c837e18923c865f6cc5e52a102c3a488f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= Date: Fri, 16 Sep 2022 23:57:24 +0200 Subject: [PATCH] No path added to @RequestMapping when JAX-RS controller defines no @Path on method --- .../actions/ConvertJaxRsAnnotations.java | 10 +- .../actions/ConvertJaxRsAnnotationsTest.java | 94 +++++++++++++++++++ 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotations.java b/components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotations.java index 9c650f4ac..204112e1d 100644 --- a/components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotations.java +++ b/components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotations.java @@ -55,7 +55,7 @@ private void transformMethodAnnotations(Type type) { .forEach(this::convertJaxRsMethodToSpringMvc); } - private void convertJaxRsMethodToSpringMvc(Method method) { + void convertJaxRsMethodToSpringMvc(Method method) { Map attrs = new LinkedHashMap<>(); Set methods = new LinkedHashSet<>(); var annotations = method.getAnnotations(); @@ -127,11 +127,13 @@ private void convertJaxRsMethodToSpringMvc(Method method) { } sb.append(attrs.entrySet().stream() - .map(e -> e.getKey() + " = " + e.getValue().print()) - .collect(Collectors.joining(", "))); + .map(e -> e.getKey() + " = " + e.getValue().print()) + .collect(Collectors.joining(", "))); if (!methods.isEmpty()) { - sb.append(", "); + if(!attrs.entrySet().isEmpty()) { + sb.append(", "); + } if (methods.size() == 1) { sb.append("method = RequestMethod." + methods.iterator().next()); } else { diff --git a/components/sbm-recipes-jee-to-boot/src/test/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotationsTest.java b/components/sbm-recipes-jee-to-boot/src/test/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotationsTest.java index ddfe0808c..bdc130fa0 100644 --- a/components/sbm-recipes-jee-to-boot/src/test/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotationsTest.java +++ b/components/sbm-recipes-jee-to-boot/src/test/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotationsTest.java @@ -15,8 +15,11 @@ */ package org.springframework.sbm.jee.jaxrs.actions; +import org.intellij.lang.annotations.Language; import org.springframework.sbm.java.api.JavaSource; import org.springframework.sbm.engine.context.ProjectContext; +import org.springframework.sbm.java.api.Method; +import org.springframework.sbm.java.migration.conditions.HasTypeAnnotation; import org.springframework.sbm.project.resource.TestProjectContext; import org.junit.jupiter.api.Test; @@ -24,6 +27,97 @@ public class ConvertJaxRsAnnotationsTest { + private final static String SPRING_VERSION = "5.3.13"; + + @Test + void convertJaxRsMethodWithoutPathToSpringMvc() { + @Language("java") + String restControllerCode = """ + package com.example.jeerest.rest; + + import com.example.jeerest.Movie; + import com.example.jeerest.MoviesBean; + import org.springframework.beans.factory.annotation.Autowired; + + import javax.ws.rs.DELETE; + import javax.ws.rs.GET; + import javax.ws.rs.PUT; + import javax.ws.rs.Path; + import javax.ws.rs.PathParam; + import javax.ws.rs.Produces; + import javax.ws.rs.QueryParam; + import javax.ws.rs.core.MediaType; + import java.util.List; + + @Path("movies") + @Produces({"application/json"}) + public class MoviesRest { + @GET + public List getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max, + @QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) { + return service.getMovies(first, max, field, searchTerm); + } + } + """; + + ProjectContext context = TestProjectContext + .buildProjectContext() + .withJavaSources(restControllerCode) + .withBuildFileHavingDependencies("org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final", + "org.springframework:spring-core:" + SPRING_VERSION) + .build(); + + Method jaxRsMethod = context.getProjectJavaSources() + .list() + .get(0) + .getTypes() + .get(0) + .getMethods() + .get(0); + + ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations + .builder() + .condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build()) + .description("Convert JAX-RS annotations into Spring Boot annotations.") + .build(); + + convertJaxRsAnnotations.convertJaxRsMethodToSpringMvc(jaxRsMethod); + + + @Language("java") + String expected = + """ + package com.example.jeerest.rest; + + import com.example.jeerest.Movie; + import com.example.jeerest.MoviesBean; + import org.springframework.beans.factory.annotation.Autowired; + + import javax.ws.rs.DELETE; + import javax.ws.rs.PUT; + import javax.ws.rs.Path; + import javax.ws.rs.PathParam; + import javax.ws.rs.Produces; + import javax.ws.rs.QueryParam; + import javax.ws.rs.core.MediaType; + import java.util.List; + + @Path("movies") + @Produces({"application/json"}) + public class MoviesRest { + @RequestMapping(method = RequestMethod.GET) + public List getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max, + @QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) { + return service.getMovies(first, max, field, searchTerm); + } + } + """; + + assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo( + expected + ); + } + @Test void replaceMethodAnnotationsWithOneAnnotation() throws Exception {