繁体   English   中英

如何测试Spring Webmvc WebService Rest

[英]How to test Spring Webmvc webservice rest

我对Spring Webmvc有一个小问题。 我正在尝试使用宁静的Web服务来制作Web应用程序,但是我不知道它是否正常运行。

我有一个带有此类的maven-webapp项目:

...
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
...

@RestController
@RequestMapping("/service")
public class PersoWsRest {

    @RequestMapping(value = "/lister", method = RequestMethod.GET)
    public List<Perso> listerPersos() {
        System.out.println("PASSAGE DS LE WEB SERVICE\n");

        List<Perso> res = new ArrayList<Perso>();

        res.add(new Perso("Test", "Un"));
        res.add(new Perso("Test", "Deux"));
        res.add(new Perso("Test", "Trois"));

        return res;
    }

}

并且,当我启动tomcat服务器时(没有错误),我无法尝试使用Web服务。

我认为这是两个问题:

  • 我没有使用正确的URL,但是我正在尝试很多URL ...所以...

  • 我没有对Spring WebMvc进行正确的配置。

你有想法吗 ? 谢谢。

对于那些正在寻找从此处开始的示例代码的人。

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(PersoWsRest.class)
@AutoConfigureMockMvc
public class PersoWsRestTest{

 @Autowired
 MockMvc mockMvc;

 @Test
public void testListerPersos(){
    MvcResult mvcResult = this.mockMvc.perform(get("/service/lister")
            .andExpect(status().isOk())
            .andReturn();
//..... Further validation
      }    
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM