0

I use spring mvc to create a rest web service. However, I always get 404. I don't have any idea where I go wrong.

Here is my Initializer:

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class<?>[] { RootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return new Class<?>[] { WebConfig.class };
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
        return new String[] { "/" };
    }

}

Here is my Webconfig:

@Configuration
@EnableWebMvc
@ComponentScan("com.jh.dummy.web")
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResovler = new InternalResourceViewResolver();
        viewResovler.setPrefix("/");
        viewResovler.setSuffix(".html");
        viewResovler.setExposeContextBeansAsAttributes(true);
        return viewResovler;
    }

    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

Here is my rest server file:

    @RestController
@RequestMapping("/v1/")
public class PositionService {
    @RequestMapping(value = "position", consumes = MediaType.APPLICATION_JSON_VALUE, method = POST)
    public void create(List<Map<String, String>> data) {
    }

    @RequestMapping(value = "position/{id:\\d+}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getById(@PathVariable("id") Long id) {
    }

    @RequestMapping(value = "position/title/{title:.+}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getByTitle(@PathVariable("title") String title,
            @PathVariable("start") int start) {
    }

@RequestMapping(value = "position/title/{title:.+}/address/{address:\\w{1,}}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
public void getByTitleAndLocation(@PathVariable("title") String title,
        @PathVariable("address") String address, @PathVariable("start") int start) {

    }

    @RequestMapping(value = "position/address/{address:\\w{1,}}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getByLocation(@PathVariable("address") String address,
            @PathVariable("start") int start) {
    }

    @RequestMapping(value = "position/company/{company:.{1,}}/{start:\\d{1,}}", produces = MediaType.APPLICATION_JSON_VALUE, method = GET)
    public void getByCompany(@PathVariable("company") String company,
                @PathVariable("start") int start) {
        }

    }

Here is the request:

enter image description here

When I use curl test, I always get 404. I can't find where goes wrong. Any idea?

Junbang Huang
  • 1,927
  • 19
  • 26

1 Answers1

0

I solve the problem. It turns out I need to add /dummy/ into the url. Therefore the url will be "localhost:8080/dummy/v1/position/v1". But in my other projects cases, I don't need to add the project name into the dummy. I still don't know why I need to do that this time. I will keep you guy posted when I found out.

Junbang Huang
  • 1,927
  • 19
  • 26
  • 1
    Finally get it right, the reason is that I forgot to set application to "/", therefore I have to add the project name in the url – Junbang Huang Apr 11 '16 at 21:06