Skip to main content

Migrate JAX-RS Response to Spring MVC ResponseEntity

org.openrewrite.java.spring.mvc.JaxrsToSpringMvcResponseEntity

Replaces all JAX-RS Response with Spring MVC ResponseEntity.

Recipe source

GitHub: JaxrsToSpringMvcResponseEntity.java, Issue Tracker, Maven Central

info

This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.

This recipe is available under the Moderne Source Available License.

Definition

  • Change type
    • oldFullyQualifiedTypeName: javax.ws.rs.core.Response.Status
    • newFullyQualifiedTypeName: org.springframework.http.HttpStatus
    • ignoreDefinition: true
  • Change type
    • oldFullyQualifiedTypeName: jakarta.ws.rs.core.Response.Status
    • newFullyQualifiedTypeName: org.springframework.http.HttpStatus
    • ignoreDefinition: true
  • Change type
    • oldFullyQualifiedTypeName: javax.ws.rs.core.Response.ResponseBuilder
    • newFullyQualifiedTypeName: org.springframework.http.ResponseEntity.BodyBuilder
    • ignoreDefinition: true
  • Change type
    • oldFullyQualifiedTypeName: jakarta.ws.rs.core.Response.ResponseBuilder
    • newFullyQualifiedTypeName: org.springframework.http.ResponseEntity.BodyBuilder
    • ignoreDefinition: true
  • Change type
    • oldFullyQualifiedTypeName: javax.ws.rs.core.Response
    • newFullyQualifiedTypeName: org.springframework.http.ResponseEntity
    • ignoreDefinition: true
  • Change type
    • oldFullyQualifiedTypeName: jakarta.ws.rs.core.Response
    • newFullyQualifiedTypeName: org.springframework.http.ResponseEntity
    • ignoreDefinition: true

Used by

This recipe is used as part of the following composite recipes:

Example

Before
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

class TestExample {

class TestResponse {
private String message;

TestResponse(String message) {
this.message = message;
}

String getMessage() {
return message;
}
}

Response test0() {
return Response.ok().build();
}

Response test1() {
return Response.ok("Test Response").build();
}

Response test2() {
TestResponse response = new TestResponse("Test Response");
return Response.ok().entity(response).build();
}

Response test3() {
return Response.ok().entity("Test Response").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
}

Response test4() {
TestResponse response = new TestResponse("Test Response");
return Response.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(response).build();
}

Response test5() {
return Response.status(Response.Status.UNAUTHORIZED).build();
}

Response test6() {
return Response.status(Response.Status.CREATED).entity("Test Response").build();
}

Response test7() {
TestResponse response = new TestResponse("Test Response");
return Response.status(Status.NOT_FOUND).entity(response).build();
}

Response test8() {
return Response.status(Status.BAD_GATEWAY).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity("Test Response").build();
}

Response test9() {
return Response.serverError().entity("Test Response").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
}

Response test10() {
return Response.noContent().build();
}

Response.ResponseBuilder test11() {
return Response.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
}

}
After
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;

class TestExample {

class TestResponse {
private String message;

TestResponse(String message) {
this.message = message;
}

String getMessage() {
return message;
}
}

ResponseEntity test0() {
return ResponseEntity.ok().build();
}

ResponseEntity test1() {
return ResponseEntity.ok("Test Response");
}

ResponseEntity test2() {
TestResponse response = new TestResponse("Test Response");
return ResponseEntity.ok().body(response);
}

ResponseEntity test3() {
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).body("Test Response");
}

ResponseEntity test4() {
TestResponse response = new TestResponse("Test Response");
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).body(response);
}

ResponseEntity test5() {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}

ResponseEntity test6() {
return ResponseEntity.status(HttpStatus.CREATED).body("Test Response");
}

ResponseEntity test7() {
TestResponse response = new TestResponse("Test Response");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}

ResponseEntity test8() {
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).body("Test Response");
}

ResponseEntity test9() {
return ResponseEntity.internalServerError().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).body("Test Response");
}

ResponseEntity test10() {
return ResponseEntity.noContent().build();
}

BodyBuilder test11() {
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
}

}

Usage

This recipe has no required configuration options. Users of Moderne can run it via the Moderne CLI.

You will need to have configured the Moderne CLI on your machine before you can run the following command.

shell
mod run . --recipe JaxrsToSpringMvcResponseEntity

If the recipe is not available locally, then you can install it using:

mod config recipes jar install org.openrewrite.recipe:rewrite-spring:6.27.1

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Source files that had results

org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

Column NameDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.