【问题标题】:Spring REST Error弹簧 REST 错误
【发布时间】: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();
}

【问题讨论】:

标签: json spring rest spring-mvc


【解决方案1】:

在方法参数前加上@PathVariable注解,让方法看起来像

@Override
public Contract getContract(@PathVariable String type) {
    Contract c = this.restTemplate.getForObject(
            "http://localhost:8080/ws-dispatcher/contract/{type}", Contract.class, type);
    return c;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 2013-12-06
    • 2012-12-22
    • 2011-07-25
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多