当我从JUnit测试用例向Rest服务发送请求时,我收到400 Bad Request。 PFB我的代码。错误日志即将结束。请帮助我解决问题。当我通过直接在URL中传递参数来调用服务器时,我没有收到任何错误:restTemplate.getForObject(BASE_URL +“/ remote?serviceName = simpleService& source = WEB”,HelloWorld.class)。但是如何使用地图,这就是问题的来源。
我的控制器:
@Controller
@RequestMapping("/remote")
public class RESTController implements IController{
@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/xml, application/json")
public @ResponseBody HelloWorld getMessage(@RequestParam("serviceName") String serviceName, @RequestParam("source") String source) throws ApplicationException{
System.out.println("got the request:");
serviceLocator serviceLocator = new serviceLocator();
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage(serviceLocator.getMessage(serviceName));
return helloWorld;
}
我的配置:
<context:annotation-config />
<context:component-scan base-package="com" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="jsonViewResolver"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="JacksonObjectMapper" />
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg value="application" />
<constructor-arg value="json" />
<constructor-arg
value="#{T(java.nio.charset.Charset).forName('UTF-8')}" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
我的控制器测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContextTest.xml")
public class RESTControllerTest {
private static final String BASE_URL = "http://localhost:8080/SimpleServiceWeb/rest";
@Autowired
private RestTemplate restTemplate;
@Test
public void getMessage() throws Exception {
String expected = "Hello Spring!";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Collections
.singletonList(MediaType.APPLICATION_XML));
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("serviceName", "simpleService");
map.add("source", "WEB");
ObjectMapper mapper = new ObjectMapper();
ObjectWriter normalWriter = mapper.writer();
String jsonString = normalWriter.writeValueAsString(map);;
HttpEntity<Object> requestEntity = new HttpEntity<Object>(httpHeaders);
HelloWorld helloWorld = restTemplate.exchange(BASE_URL + "/remote",
HttpMethod.GET, requestEntity, HelloWorld.class, map) .getBody();
assertThat(helloWorld.getMessage(), is(expected));
}
}
我的测试配置:
.simple“/&gt;
<mvc:annotation-driven />
<bean id="springServiceLocator" class="com.servicelocator.simple.SpringServiceLocator">
<property name="simpleService" ref="simpleService" />
</bean>
<bean id="simpleService" class="com.service.simple.SimpleService" />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="jsonViewResolver"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="JacksonObjectMapper" />
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg value="application" />
<constructor-arg value="json" />
<constructor-arg
value="#{T(java.nio.charset.Charset).forName('UTF-8')}" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg ref="httpClientParams" />
</bean>
<bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams">
<property name="connectionManagerClass"
value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager" />
</bean>
<bean id="httpClientFactory"
class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<constructor-arg ref="httpClient" />
</bean>
错误:
org.springframework.web.client.HttpClientErrorException:400 Bad Request at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:75) 在org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486) 在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443) 在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) 在org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:384) at test.com.servicefacade.simple.RESTControllerTest.getMessage(RESTControllerTest.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 在org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) 在org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222) 在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 在org.junit.runners.ParentRunner.run(ParentRunner.java:300) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
答案 0 :(得分:0)
可能是由于缺少xml转换器,Spring无法正确封送您的请求,因此给出400?虽然如控制器中所述支持媒体类型,但这就是为什么你没有得到415。