0

This instance of class AuthorizationRequest is created during HTTP request, params are sent in query string.

@RequestMapping(value = "/authorize", method = {RequestMethod.GET, RequestMethod.POST})
    public String authorize(
            @Valid AuthorizationRequest authorizationRequest,
            BindingResult result
    ) {

I would like to use this code, this is an example parameter from AuthorizationRequest class:

@NotEmpty
@JsonProperty("client_id")
private String clientId;

but new instance has a filed clientId empty, because in query string there is a value for this parameter under client_id parameter.

Is there some way how to tell Spring which parameter from HTTP request should use for one particular field of created instance? I need to solve problem with different naming clientId andclient_id`.

2 Answers 2

0

What you need is a setter to handle each kind of clientId. Keep in mind that if both clientId and client_id is specified that it is unknown which will take precedence.

//These methods will allow clientId or client_id 
to be used as arguments setting the same field this.clientId
public void setClient_id(String client_id) {
    this.clientId = client_id;
}

public void setClientId(String client_id) {
    this.clientId = client_id;
}

I tested this with a post and a get

get - http://localhost:8080/authorize?clientId=2&username=someusername
get - http://localhost:8080/authorize?client_id=2&username=someusername

post - http://localhost:8080/authorize 
Content-Type: application/x-www-form-urlencoded 
Body: clientId=2&username=someusername

or Body: client_id=2&username=someusername

I was only able to have @JsonProperty("client_id") to be recognized when I annotated AuthorizationRequest with @RequestBody and then used application/json instead of application/x-www-form-urlencoded

Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution with own implementation of org.springframework.web.method.supportHandlerMethodArgumentResolver.

Resolver implementation:

public class AuthorizationRequestResolver implements HandlerMethodArgumentResolver {

    private static Logger LOG = Logger.getLogger(AuthorizationRequestResolver.class);

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().equals(AuthorizationRequest.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {

        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

        AuthorizationRequest authRequest = mapFromServletRequest(request);
        return authRequest;

    }

    private AuthorizationRequest mapFromServletRequest(HttpServletRequest request) {
        AuthorizationRequest authorizationRequest = new AuthorizationRequest();
        authorizationRequest.setClientId(request.getParameter("client_id"));
        authorizationRequest.setRedirectUri(request.getParameter("request_uri"));
        authorizationRequest.setResponseType(request.getParameter("response_type"));
        authorizationRequest.setScope(request.getParameter("scope"));
        authorizationRequest.setState(request.getParameter("state"));
        return authorizationRequest;
    }

}

and cofiguration class:

@Configuration
public class WebappConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new AuthorizationRequestResolver());
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.