There is [Fromuri] equivalent in Java Spring MVC

Asked

Viewed 29 times

1

In ASPNET MVC, we have the following code snippet that sends information through queryString and is received in an endpoint which has a parameter of type Dtorequest and through the attribute [Fromuri] is made the auto-Inding for the properties

        [HttpGet]
        [Route("search")]
        public Task<HttpResponseMessage> Search([FromUri] DTORequest request)
        {
            var result= _MyService.Search(request);
            return base.CreateSimpleResponse(HttpStatusCode.OK, result);
        }

There is something equivalent in Java with Spring MVC for the same purpose ?

        @GetMapping("search")
        public ResponseEntity<?> Search(@FromUri DTORequest request)
        {
            var result= _MyService.Search(request);
            return base.CreateSimpleResponse(HttpStatusCode.OK, result);
        }

1 answer

0

You don’t need anything equivalent, Spring does it himself. Your object needs to have a public constructor with the parameters or an empty public constructor and setters;

@RestController
public class Controller {

    @GetMapping("/teste")
    public ResponseEntity<String> teste(ObjetoTeste params) {
        return ResponseEntity.ok(params.toString()); 
    }
    
    @Data // construtor/getter/setters/toString
    class ObjetoTeste {
        private Integer i;
        private String s;
    }
}

// request = /teste?i=10&s=texto
// saída = Controller.ObjetoTeste(i=10, s=texto)
  • Top !! thanks @Matthew...worked out brother

Browser other questions tagged

You are not signed in. Login or sign up in order to post.