2016-03-14 31 views
0

我对Spring Webmvc有一些疑问。我试图用一个平静的web服务制作一个web应用程序,但我不知道它是否正确工作。如何测试Spring Webmvc 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的...所以...

  • 我不t使Spring WebMvc的配置正确。

你有什么想法吗?谢谢。

+2

请指定3件事情:您的Web应用程序上下文路径(Web应用程序路径),调度程序Servlet的URL映射以及您用于访问此服务的URL。 –

+0

在任何人都可以帮到你之前,你需要添加更多的细节 –

+0

你的本地tomcat运行8080的端口是什么?上下文路径是什么?该URL应该像localhost:8080/webappcontext/server/lister,但没有更多的细节很难提供帮助。 – slarge

回答

0

对于那些正在寻找示例代码的人来说,这里是。

@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 
     }  
    }