RestTemplate内容类型'application / json'不受支持

时间:2018-03-27 00:12:47

标签: json spring rest spring-mvc

我正在尝试将POJO转换为JSON,将其发布到将返回String的休息控制器。我有以下客户端和服务器的代码。当我运行客户端时,我得到并且错误声明“内容类型'应用程序/ json'不受支持”。我在客户端和服务器上的pom中都有jackson,不确定在哪里尝试

的pom.xml: -

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
</dependency>

休息客户代码: -

public void process() {
    ScriptConfiguration script = new ScriptConfiguration();
    String url = "http://localhost:8080/script-runner/run";
    RestTemplate restTemplate = new RestTemplate();

    // Add MappingJackson2HttpMessageConverter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    // Prepare HTTPHeaders
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    // Prepare HttpEntity
    HttpEntity<String> entity = new HttpEntity<>(headers);

    // Invoke the REST service
    ResponseEntity<String> result = restTemplate.exchange(url,HttpMethod.GET, entity, String.class, script);

    // Get the JSON response body
    System.out.println(result.getBody());
}

RestController

@RestController
public class SynchronousRestController {

@ResponseBody
@RequestMapping(value="/run", consumes = MediaType.APPLICATION_JSON_VALUE)
public String submit(@RequestBody ScriptConfiguration script) throws IOException {

    LOG.info("Received: {}", script);

}

日志

RequestHeaders: {Accept=[text/plain, application/json, 
application/json, application/*+json, application/*+json, text/plain, */*, */*], Content-Type=[application/json], Content-Length=[320]}
RequestMethod: POST

ScriptConfiguration

public class ScriptConfiguration implements Serializable {

    private static final long serialVersionUID = -1504819055948935133L;
    private File specimenTdf;
    private File cliInput;
    private File workingDirectory;
    private File workspace;
    private File stdErr;
    private File stdOut;
    private List<String> args;
    private Map<String, String> environmentArgs;

    public File getSpecimenTdf() {
        return specimenTdf;
    }
    public void setSpecimenTdf(File specimenTdf) {
        this.specimenTdf = specimenTdf;
    }
    public File getCliInput() {
        return cliInput;
    }
    public void setCliInput(File cliInput) {
        this.cliInput = cliInput;
    }
    public File getWorkingDirectory() {
        return workingDirectory;
    }
    public void setWorkingDirectory(File workingDirectory) {
        this.workingDirectory = workingDirectory;
    }
    public File getWorkspace() {
        return workspace;
    }
    public void setWorkspace(File workspace) {
        this.workspace = workspace;
    }
    public File getStdErr() {
        return stdErr;
    }
    public void setStdErr(File stdErr) {
        this.stdErr = stdErr;
    }
    public File getStdOut() {
        return stdOut;
    }
    public void setStdOut(File stdOut) {
        this.stdOut = stdOut;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    public List<String> getArgs() {
        return args;
    }
    public void setArgs(List<String> args) {
        this.args = args;
    }
    public Map<String, String> getEnvironmentArgs() {
        return environmentArgs;
    }
    public void setEnvironmentArgs(Map<String, String> environmentArgs) {
        this.environmentArgs = environmentArgs;
    }
}

1 个答案:

答案 0 :(得分:0)

您的方法类型应为POST。将控制器方法更改为:

@ResponseBody
@RequestMapping(value="/run", consumes = MediaType.APPLICATION_JSON_VALUE, method = POST)
public String submit(@RequestBody ScriptConfiguration script) throws IOException {
    LOG.info("Received: {}", script);
    return "success";
}

在您的客户中更改此内容:

// Invoke the REST service
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(script, headers), String.class);
相关问题