【发布时间】:2015-12-27 01:40:10
【问题描述】:
我正在尝试使用我使用 Spring framework 和 REST `JSON 创建的 Web 服务,但我收到此错误:
( AVERTISSEMENT: GET request for "http://localhost:8080/ws-dispatcher/contract/typeA" 导致 404 (Introuvable);调用错误处理程序 线程“主”org.springframework.web.client.HttpClientErrorException 中的异常:404 Introuvable 在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76) 在 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:401) 在 org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199) 在 com.sqli.formation.spring.ws.client.impl.DefaultWebServiceClientImpl.getContract(DefaultWebServiceClientImpl.java:18) 在 com.sqli.formation.spring.ws.client.AppLauncher.(AppLauncher.java:15) 在 com.sqli.formation.spring.ws.client.AppLauncher.main(AppLauncher.java:24)
服务实现
package com.sqli.formation.spring.ws.client.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.sqli.formation.spring.ws.client.IWebServiceClient;
import com.sqli.formation.spring.ws.client.model.Contract;
@Service("defaultWebServiceClient")
public class DefaultWebServiceClientImpl implements IWebServiceClient {
@Autowired
private RestTemplate restTemplate;
@Override
public Contract getContract(String type) {
Contract c = this.restTemplate.getForObject(
"http://localhost:8080/ws-dispatcher/contract/{type}", Contract.class, type);
return c;
}
}
这是 Application-context.xml :
<context:component-scan base-package="com.sqli.formation.spring.ws"></context:component-scan>
<!-- Déserialiseur JSON -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="jacksonConverter"/>
</list>
</property>
</bean>
<bean id="jacksonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
这是应用启动器:
public AppLauncher() {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
IWebServiceClient wsClient = context.getBean("defaultWebServiceClient", IWebServiceClient.class);
Contract c = wsClient.getContract("typeA");
System.out.println(c);
}
/**
* @param args
*/
public static void main(String[] args) {
new AppLauncher();
}
【问题讨论】:
-
在您的浏览器中是否可以使用 URL“localhost:8080/ws-dispatcher/contract/typeA”的 GET 调用?
-
@AnthonyRaymond 否,因为在那个控制器中我只返回它的 id = "typeA" 的对象
标签: json spring rest spring-mvc