-1

i am studying spring 5 and i can not use @RequestMapping annotation and don't know why

@RequestMapping includes @Component annotation so I just thought I can use that

initRequest includes URL parameter by string

i just expected initRequest(/hello) parameter binds URL

here is my code

public class SimpleControllerTest extends AbstractDispatcherServletTest {
@Test
public void helloSimpleController() throws ServletException, IOException {
    setClasses(HelloController.class);
    initRequest("/hello").addParameter("name", "spring");
    runService();
    assertModel("message", "Hello spring");
    assertViewName("/WEB-INF/view/hello.jsp");
}

@Test(expected=Exception.class)
public void noParameterHelloSimpleController() throws ServletException, IOException {
    setClasses(HelloController.class);
    initRequest("/hello");
    runService();
}

@Component("/hello")
//@RequestMapping("/hello")
static class HelloController extends SimpleController {
    public HelloController() {
        this.setRequiredParams(new String[] {"name"});
        this.setViewName("/WEB-INF/view/hello.jsp");
    }

    public void control(Map<String, String> params, Map<String, Object> model) throws Exception {
        model.put("message", "Hello " + params.get("name"));
    }
}

static abstract class SimpleController implements Controller {
    private String[] requiredParams;
    private String viewName;

    public void setRequiredParams(String[] requiredParams) {
        this.requiredParams = requiredParams;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }

    final public ModelAndView handleRequest(HttpServletRequest req,
                                            HttpServletResponse res) throws Exception {
        ...
    }


    public abstract void control(Map<String, String> params, Map<String, Object> model) throws Exception;
}

}

  • What is `AbstractDispatcherServletTest`? Why are you not using `MockMvc`? What `Controller` class are you using, since it's not Spring MVC's Controller? Start with a basic Spring MVC tutorial, because it will walk you through the fundamentals you need to learn. – chrylis -cautiouslyoptimistic- Jun 19 '20 at 22:21
  • AbstractDispatcherServletTest class contains mock objects MockHttpServletRequest , MockHttpServletResponse , MockServletConfig ,MockHttpSession and dispatcherservlet – lovelydays95 Jun 20 '20 at 02:36
  • this is my error code org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/hello] in DispatcherServlet with name 'spring' – lovelydays95 Jun 20 '20 at 02:42

1 Answers1

0

You need to work on your Spring basics. Your understanding of which annotations do what is incorrect and incomplete. The following links provide good knowledge on these. Go through these, revise your code, and you will solve this problem without needing help.

Spring Framework Annotations

Spring Annotations - JournalDev

Meet K.
  • 459
  • 3
  • 15