有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在名为“”的DispatcherServlet中找不到URI为[/api/encodedurl]的HTTP请求的java映射

我正在为一个API编写JUnit测试用例,但得到的错误是No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''
我浪费了两天多的时间没有检查任何解决方案,所有可能的事情都没有检查。API已经是无令牌的

这是我的密码

编码器静止测试

package com.zoylo.admin.web.rest;

import static org.junit.Assert.assertEquals;    
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 

@RunWith(SpringRunner.class)
@SpringBootTest(classes = EncoderRest.class)
@AutoConfigureMockMvc
public class EncoderRestTest {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private EncoderBll encoderBll;

    ObjectMapper mapper = new ObjectMapper();

    @Test
    public void save() throws Exception {
        // mock output
        EncoderMV mv = new EncoderMV();
        mv.setPaymentUrl("www.paytm.com");
        Mockito.when(encoderBll.createPaymentUrl(Mockito.any(EncoderVM.class))).thenReturn(mv);

        EncoderVM vm = new EncoderVM();
        vm.setBookingId("ICICI090");
        vm.setType("Payment");

        String requestBody = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(vm);
        RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/api/encodedurl")
                .accept(MediaType.APPLICATION_JSON).content(requestBody).contentType(MediaType.APPLICATION_JSON);
        MvcResult result = mockMvc.perform(requestBuilder).andReturn();
        MockHttpServletResponse response = result.getResponse();
        assertEquals(HttpStatus.CREATED.value(), response.getStatus());

    }

}

EncoderRest

@RestController
@RequestMapping("/api")
public class EncoderRest {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EncoderBll encoderBll;


    @PostMapping("/encodedurl")
    public ResponseEntity<?> generateEncodedUrl(@RequestBody EncoderVM encoderVM) {
        ......
    }
}

EncoderBllImpl

public class EncoderBllImpl implements EncoderBll{

    @Override
    public EncoderMV createPaymentUrl(EncoderVM encoderVM) {
        EncoderMV encoderMV = null;
        .
        .
        .   
        return encoderMV;
    }   
}

错误

No mapping found for HTTP request with URI [/api/encodedurl] in DispatcherServlet with name ''  

共 (1) 个答案

  1. # 1 楼答案

    您确定在测试期间启动的应用程序上下文中存在rest控制器吗

    试试@SpringBootTest(classes = TestConfig.class)而不是@SpringBootTest(classes = EncoderRest.class)

    @Configuration
    @EnableAutoConfiguration
    public class TestConfig {
    
    }