如何使用@RequestMapping在Spring MVC Controller中优化我的代码?

前端之家收集整理的这篇文章主要介绍了如何使用@RequestMapping在Spring MVC Controller中优化我的代码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在我的控制器中,我的控制器方法名称等于请求映射URL.例如,/ list等于方法名称列表.是否有一个通用的处理程序方法来缩短我的代码?我不想以这种方式编写每个控制器和方法.我记得.net mvc有一个简单的方法来配置它.关于Spring MVC怎么样?

@Controller
@RequestMapping(value = "/fooController ")
public class FooController {
     @RequestMapping("/list") public String list(...) { ... }
     @RequestMapping("/save") public String save(...) { ... }
     @RequestMapping("/delete") public String delete(...) { ... }
}

@Controller
@RequestMapping(value = "/basketballController ")
public class BasketballController {
     @RequestMapping("/list") public String list(...) { ... }
     @RequestMapping("/save") public String save(...) { ... }
     @RequestMapping("/delete") public String delete(...) { ... }
}
最佳答案
您可以使用RequestMappingHandlerMapping并覆盖默认代码

protected RequestMappingInfo getMappingForMethod(Method method,Class

正如您在此处所看到的,它尝试从方法中解析RequestMapping注释并与Controller类注释结合使用.

只需替换逻辑即可使用方法名称.

here一个类似的逻辑.而不是方法名称使用安全检查.

更新:

要测试的类.对我来说它有效.
MappingHandler我使用方法名称检查,因为有更多的控制器,错误控制器等.对于真正的解决方案,我将在控制器上引入一个注释,以从逻辑中排除默认的弹簧控制器

public class ExtendedRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    protected RequestMappingInfo getMappingForMethod(Method method,Class

配置使用映射

@Configuration
public class ExtendedWebMvcConfiguration extends WebMvcConfigurationSupport {

    @Override @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        ExtendedRequestMappingHandlerMapping handlerMapping = new ExtendedRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);
        handlerMapping.setInterceptors(getInterceptors());
        return handlerMapping;
    }

    @Override @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter();
        adapter.setIgnoreDefaultModelOnRedirect(true);
        return adapter;
    }

}

调节器

@RestController

@RequestMapping("/common")
public class MethodNameController {
    public String test() {
        return "test";
    }
    public String test2() {
        return "test2";
    }
}

考试班

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MethodNameControllerTest {
    @LocalServerPort
    private int port;

    @Value("${server.contextPath}")
    private String contextPath;
    private String base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        this.base = "http://localhost:" + port;
    }

    @Test
    public void testMethodNameMappingResolving() throws Exception {
        TestRestTemplate template = new TestRestTemplate();
        String url = base + contextPath + "/common/test";
        String res1 = template.getForObject(url,String.class);
        assertThat(res1,equalTo("test"));

        url += "2";
        String res2 = template.getForObject(url,String.class);
        assertThat(res2,equalTo("test2"));
    }

}
原文链接:https://www.f2er.com/spring/432402.html

猜你在找的Spring相关文章