【问题标题】:No Mapping GET for URI.json formatURI.json 格式没有映射 GET
【发布时间】:2020-07-27 07:30:36
【问题描述】:

我不知道为什么 URI.json 的 GET 消息没有映射..

但没有'.json'也能很好地工作

我尝试在 Junit4 中使用 MockMvc 测试。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class WriteActionControllerTest {

    @Autowired
    WriteActionController writeActionController;

    MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(writeActionController).build();

    }

    @Test
    void getCommentsList() throws Exception {

        RequestBuilder requestBuilder = 
        MockMvcRequestBuilders.get("/view/2/comments.json").contentType(MediaType.APPLICATION_JSON);
        mockMvc.perform(requestBuilder).andExpect(status().isOk()).andDo(print());
    }

这段代码有SpringController.class

@RestController
public class WriteActionController {


    @GetMapping("/view/{postNumber}/comments")
    public List<CommentsDTO> getCommentsList(@PathVariable int postNumber){
        return viewService.select_CommentsByPostNumber(postNumber);
    }

}

但是,我在 Junit4 中使用 MockMvcTest 时收到了这个成功的 ResponseMessage。 我想回答为什么 GET 请求在浏览器上不起作用

MockHttpServletRequest:
  HTTP Method = GET
  Request URI = /view/2/comments.json
   Parameters = {}
      Headers = [Content-Type:"application/json"]
         Body = <no character encoding set>
Session Attrs = {}




MockHttpServletResponse:
       Status = 200
       Error message = null
       Headers = [Content-Type:"application/json"]
       Content type = application/json
       Body = [{"comments_id":1,"comments_content":"hello world","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":2,"comments_content":"test","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":3,"comments_content":"uytyu","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":4,"comments_content":"tewqwes","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":5,"comments_content":"testtest","_usr_email":"mokaim@naver.com","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":6,"comments_content":"testtest","_usr_email":"mokaim@naver.com","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":7,"comments_content":"test","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"},{"comments_id":8,"comments_content":"wewer","_usr_email":"admin","_post_num":2,"reg_date":"2020-07-26"}]
      Forwarded URL = null
      Redirected URL = null
      Cookies = []

【问题讨论】:

  • 因为你的测试和实际代码不同。您应该自动连接 MockMvc 以获得与重用实际 Web 配置相同的结果,您的配置将创建一个新配置。

标签: json spring rest


【解决方案1】:

嗯,我不太确定我是否明白你的问题。是关于 RequestMapping 的问题吗?所以你在这里有这个控制器:

@GetMapping("/view/{postNumber}/comments")

所以你的应用程序只监听comments 而不是comments.json。如果需要,您可以向映射添加多个值。

@RequestMapping(value={"/view/{postNumber}/comments", "/view/{postNumber}/comments.json"})

这里还有Baeldung Link,它给出了关于 Spring 的精彩解释。

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 2019-09-27
    • 2020-01-08
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2016-01-23
    相关资源
    最近更新 更多