Search code examples
javaspring-bootjax-rsspring-restcontroller

Jax Rs to Rest Controller


I have a rest end point written using JAX-RS

 import javax.ws.rs.core.Context

    @POST
    public Response something(@RequestBody MyOrderObject obj1,@Context MyObject obj2) {
    
    }

I want to write the above rest end point using Spring Rest.What should I replace the @Context in Spring Boot ?

@RestController
     class MyController
     {
    @POST
    public @ResponseBody something(@RequestBody MyOrderObject obj1) {

    }
    }

Solution

  • The @Context is Dependency Injection featured by JAX-RS. See also: https://dzone.com/articles/jax-rs-what-is-context

    In your case just inject MyObject object2 into your class as an attribue via @Autowired so you can use it later:

    class MyController {
    
      private final MyObject object2;
    
      @Autowired
      public MyController(MyObject object2) {
        this.object2 = object2;
      }
      ...
    }