Question

The only relevant question I could I find here is this one.

Unfortunately unlike the above mentioned question I am not using file names as a path variable. What I am sending in a string which may or may not have '.'. As a result I do not want to use a strategy mentioned in the solution for the above question.

This is my WebConfigAdapter:

@Configuration
public class WebConfigAdapter extends WebMvcConfigurerAdapter {
    @Autowired
    HandlerExceptionResolver exceptionHandler;

    /**
     * Disable content negotiation for path extensions.
     * 
     * @param configurer
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            configurer.favorPathExtension(false);
    }

    /**
     * Set the exception handler.
     * 
     * @param exceptionResolvers
     */
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
            super.configureHandlerExceptionResolvers(exceptionResolvers);
            exceptionResolvers.add(exceptionHandler);
    }

    /**
     * Serve static resources.
     * 
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

I import WebConfigAdapater in my WebConfig (a java file) like so:

@Configuration
@EnableWebMvc
@Import(value = { WebConfigAdapter.class, WebConfigSupport.class })
public class WebConfig {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
    }

    @Bean
    public HandlerExceptionResolver exceptionHandler() {
            return new ExceptionHandler();
    }
}

And in my application context xml:

<bean class="com.quova.platform.portal.config.WebConfig"/>

I still cant figure out when I use Spring mock mvc in my unit tests, why the below controller receives the string input it strips off the characters after the last period in the string including the period.

 @RequestMapping(value = "/{ip}", method = RequestMethod.GET, produces = "application/json")
 @ResponseBody
 public Info lookup(@PathVariable("ip") String ip) throws Exception {

Here's how I am calling the above endpoint in my unit test:

String ip = "4.3.2";
mockMvc.perform(get("/secure/resources/iplookup/{ip}", ip)).andExpect(status().isBadRequest());

So now what the controller sees is "4.3". Also this is my first question on stack overflow, so do let me know if i am missing some needed information or can explain better my question in a better way! Thanks for the help!

Was it helpful?

Solution

@RequestMapping(value = "/{ip:.*}", method = RequestMethod.GET, produces = "application/json")
 @ResponseBody
 public Info lookup(@PathVariable("ip") String ip) throws Exception {

This works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top