Skip to content

No path set to @RequestMapping for JAX-RS method without @Path #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void transformMethodAnnotations(Type type) {
.forEach(this::convertJaxRsMethodToSpringMvc);
}

private void convertJaxRsMethodToSpringMvc(Method method) {
void convertJaxRsMethodToSpringMvc(Method method) {
Map<String, Expression> attrs = new LinkedHashMap<>();
Set<String> methods = new LinkedHashSet<>();
var annotations = method.getAnnotations();
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,109 @@
*/
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;

import static org.assertj.core.api.Assertions.assertThat;

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<Movie> 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<Movie> 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 {

Expand Down