【问题标题】:java.lang.IllegalArgumentException: Unrecognized Type: [null]java.lang.IllegalArgumentException:无法识别的类型:[null]
【发布时间】:2016-02-09 12:34:59
【问题描述】:

我在我创建的 Spring MVC 项目中有以下结构。

public class CustomerTest {
//@Mock
//private Customer customer;
 @Mock
 private CustomerService service; 

 @InjectMocks 
 private CustomersController custcontroller; 
 private MockMvc mockmvc;
/* Before executes before each and every test */
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    mockmvc = MockMvcBuilders.standaloneSetup(custcontroller).build();

}

/*test that our requests are being sent*/

@Test
public void getExistingCustomerEntry() throws Exception {
    // assertEquals(expected parameter,your parameter)
    Customer cust = new Customer();
    cust.setCustomer_id(1L);
    cust.setName("sam");
    when(service.find(1L)).thenReturn(cust);
    try {
    mockmvc.perform(get("/rest/cust-entries/1"))
    .andExpect((jsonPath("$.name", Is.is(cust.getName()))))
    .andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/cust-entries/1"))))
    .andExpect(status().isOk())
    .andDo(print());
    } catch(Exception e) {
        throw e;
    }
}
}

控制器类如下:

@Controller
public class CustomersController {
    private CustomerService service; 

public CustomersController(CustomerService service) {
    this.service = service;
}
/*used a path variable to allow to bind a variable in the url to the Java variable*/
@RequestMapping(value = "/rest/cust-entries/{customer_id}", method= RequestMethod.GET)
public ResponseEntity<CustomerResource> getCustomerEntry(@PathVariable Long customer_id) {
  Customer custEntry = service.find(customer_id);
  System.out.println("Was here" + customer_id);
  CustomerResource resource = new CustomerServiceEntryAsm().toResource(custEntry);
  return new ResponseEntity<CustomerResource>(resource,HttpStatus.OK);
}

}

运行 CustomerTest 类后我收到以下错误消息

CustomerTest

test.java.CustomerTest getExistingCustomerEntry(test.java.CustomerTest) org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.IllegalArgumentException: Unrecognized Type: [null]

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)

at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)

at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)

at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)

at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)

at test.java.CustomerTest.getExistingCustomerEntry(CustomerTest.java:74)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

原因:java.lang.IllegalArgumentException:无法识别的类型:[null]

at com.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1109)

at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:566)

at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:602)

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:311)

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:249)

at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:222)

at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183)

at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80)

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)

... 33 more

我调试了junit测试和控制器类,下一行的资源返回类型出现空参数异常

return new ResponseEntity<CustomerResource>(resource,HttpStatus.OK);

我在这里做错了什么?

【问题讨论】:

    标签: spring-mvc mockito junit4


    【解决方案1】:

    我昨天经历了这样的事情。如果你使用的是 jackson 2.7 - 任何版本的 spring 都小于 spring 4.3 - 你可能会得到这个。

    所以将 jackson 降级到 2.6.5(取决于您的 spring 版本)- 有一个很好的改变它会起作用。

    【讨论】:

    • HHmm 我刚遇到这个问题。我所做的,我使用了jackson-corejackson-annotationsjackson-datatype-jsr310 到版本2.7.1jackson-databind 到版本 2.7.1-1jackson-datatype-hibernate5 到版本 2.6.5。事情开始起作用了。我正在使用弹簧版本4.2.4.RELEASEhibernate-entitumanger 版本5.1.0.Final。对我来说罪魁祸首是jackson-datatype-hibernate5。将此降级为2.6.5 是我的解决方案。谢谢
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多