When you want to get headers, parameters, etc. from "HttpServeletRequest" For the time being
public Oject sample(HttpServletRequest request) {
//processing
}
If you do, you can do it in the controller class, Since the responsibility of the controller class increases, I wanted to bind and receive only the required values to the appropriate objects.
You should use HandlerMethodArgumentResolver
!
I just want to understand how to use HandlerMethodArgumentResolver
Try to easily receive the value of the request parameter in your own class instead of String
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
@Getter
@AllArgsConstructor
@ToString
public class SampleArgument {
private String value;
public static SampleArgument of(String value) {
return new SampleArgument(value);
}
}
For my personal taste, instantiation is possible with the ʻofmethod. A simple class that holds the string received as an argument in
value`.
HandlerMethodArgumentResolver A class for binding to any class in the subject
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
@Component
public class SampleResolver implements HandlerMethodArgumentResolver {
//Details are omitted. .. ResolveArgument is executed only if true
@Override
public boolean supportsParameter(MethodParameter parameter) {
return true;
}
//The object returned here will be available as an argument
@Override
public Object resolveArgument(
MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory
) throws Exception {
String value = webRequest.getParameter("value");
return SampleArgument.of(value);
}
}
Implements the HandlerMethodArgumentResolver
interface.
Here, @Component
is added to register as a bean.
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
@AllArgsConstructor
public class HandlerMethodArgumentResolverConfiguration implements WebMvcConfigurer {
private SampleResolver sampleResolver;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(sampleResolver);
}
}
Since I added @Component earlier, Resolver receives it in the constructor (self-propelled generation with @AllArgsConstructor
)
The point is
-Implementing the WebMvcConfigurer
interface
-Give @Configuration
to the class
After that, add the Resolver implemented in the list with the implementation method of ʻaddArgumentResolvers` and it's OK!
We have prepared the following controllers.
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/HandlerMethodArgumentResolver")
@Slf4j
public class HandlerMethodArgumentResolverController {
@GetMapping("/sample")
@ResponseBody
public SampleArgument sample(SampleArgument sampleArgument) {
log.info("sampleArgument: {}", sampleArgument);
return sampleArgument;
}
}
http://localhost:8080/HandlerMethodArgumentResolver/sample?value=HelloWorld When you access
sampleArgument: SampleArgument(value=HelloWorld)
I was able to confirm that I was able to bind to my own class! !!
Recommended Posts