(समाधान प्रश्न के अंत में तैनात)विधिपत्र विश्व स्तर पर का उपयोग कर स्प्रिंग MVC जावा आधारित एनोटेशन का उपयोग कर
मैं एक स्प्रिंग 4 MVC एप्लिकेशन बनाने रहा कॉन्फ़िगर किया गया 404 अपवाद संभाल। और यह जावा एनोटेशन का उपयोग कर पूरी तरह से कॉन्फ़िगर किया गया है। web.xml
नहीं है। अनुप्रयोग, AbstractAnnotationConfigDispatcherServletInitializer
के कहने और WebMvcConfigurerAdapter
इसलिए की तरह उपयोग करके कॉन्फ़िगर किया गया है
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example.*"})
@EnableTransactionManagement
@PropertySource("/WEB-INF/properties/application.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {
...
}
और
public class WebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
...
}
मैं अब 404 पृष्ठों यानी HttpStatus.NOT_FOUND
एक वैश्विक/सभी अपवाद संचालक को जोड़ने के लिए कोशिश कर रहा हूँ, लेकिन कोई सफलता। मैंने कोशिश की कुछ तरीकों से नीचे दिए गए हैं।
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
@ControllerAdvice
public class GlobalExceptionHandlerController {
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView handleException (NoSuchRequestHandlingMethodException ex) {
ModelAndView mav = new ModelAndView();
return mav;
}
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView handleExceptiond (NoHandlerFoundException ex) {
ModelAndView mav = new ModelAndView();
return mav;
}
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public void handleConflict() {
}
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
public void handlesdConflict() {
}
}
इनमें से कोई भी विधि निष्पादित नहीं होती है। मैं इसे संभालने के तरीके के रूप में एक नुकसान में हूँ। मैं web.xml
becasue का उपयोग नहीं करना चाहता हूं तो मुझे इसके लिए केवल एक बनाना होगा।
समाधान मैं क्या mentioned here @Sotirios किया था। यहां मेरा भाग WebAppInitializer
कोड है जो प्रेषक को ठीक से सेट करेगा।
public class WebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
...
@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
String servletName = super.getServletName();
Assert.hasLength(servletName, "getServletName() may not return empty or null");
WebApplicationContext servletAppContext = super.createServletApplicationContext();
Assert.notNull(servletAppContext,
"createServletApplicationContext() did not return an application " +
"context for servlet [" + servletName + "]");
DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
//>>> START: My custom code, rest is exqact copy of the super class
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
//>>> END
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
Assert.notNull(registration,
"Failed to register servlet with name '" + servletName + "'." +
"Check if there is another servlet registered under the same name.");
registration.setLoadOnStartup(1);
registration.addMapping(getServletMappings());
registration.setAsyncSupported(isAsyncSupported());
Filter[] filters = getServletFilters();
if (!ObjectUtils.isEmpty(filters)) {
for (Filter filter : filters) {
super.registerServletFilter(servletContext, filter);
}
}
super.customizeRegistration(registration);
}
...
}
हालांकि मैं इस समाधान के साथ विशेष रूप से खुश क्योंकि मेरी केवल कस्टम कोड 1 लाइन & बाकी सीधे सुपर वर्ग से नकल कर रहा है नहीं कर रहा हूँ। मेरी इच्छा है कि ऐसा करने के लिए एक अच्छा तरीका था।
मैं नहीं जानता कि क्यों उन्होंने ऐसा नहीं किया 'डिस्पैचर सर्वलेट' को कुछ 'संरक्षित' विधि के माध्यम से दृश्यमान बनाएं, लेकिन कस्टम समाधान अच्छा लगता है। –
प्रतिक्रिया के लिए धन्यवाद @Sotirios धन्यवाद, बस सिर्फ DispatcherServlet – Ysak