Java SpringBoot 1.5.2:当我创建PUT RequestMethod时,所需的请求主体丢失
当我想要测试REST应用程序时,我尝试编写一个测试代码段,如下所示: 控制器代码:Java SpringBoot 1.5.2:当我创建PUT RequestMethod时,所需的请求主体丢失,java,spring,spring-boot,Java,Spring,Spring Boot,当我想要测试REST应用程序时,我尝试编写一个测试代码段,如下所示: 控制器代码: import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value="/Test") public class test { @RequestMapping(value="test1/{modelId}
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value="/Test")
public class test {
@RequestMapping(value="test1/{modelId}",method = RequestMethod.POST)
public void test1(@PathVariable(value="modelId") String modelId, @RequestBody MultiValueMap<String, String> values){
String name = values.getFirst("name");
String description = values.getFirst("description");
System.out.println(modelId);
System.out.println(name);
System.out.println(description);
}
@RequestMapping(value="test2",consumes="application/x-www-form-urlencoded",method = RequestMethod.PUT)
public void test2( @RequestBody MultiValueMap<String, String> values){
String name = values.getFirst("name");
String description = values.getFirst("description");
System.out.println(name);
System.out.println(description);
}
}
当我调用http://localhost:8080/Test/test1/123
。
但是当我尝试呼叫http://localhost:8080/Test/test2
,控制台显示警告:
WARN o.s.w.s.m.support.DefaultHandlerExceptionResolver - Failed to read HTTP message:
org.springframework.http.converter.HttpMessageNotReadableException:
Required request body is missing:
public void com.wisto.util.test.test2(org.springframework.util.MultiValueMap<java.lang.String, java.lang.String>)
警告o.s.w.s.m.support.DefaultHandlerExceptionResolver-无法读取HTTP消息:
org.springframework.http.converter.httpMessageNodeTableException:
缺少必需的请求正文:
public void com.wisto.util.test.test2(org.springframework.util.MultiValueMap)
并且浏览器得到400个错误
我想我一定错过了关于SpringBoot配置的一些东西。我怎样才能解决它
为了更清楚,我放了一个来自Activiti的真实代码
包org.activiti.rest.editor.model
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* @author Tijs Rademakers
*/
@RestController
public class ModelSaveRestResource implements ModelDataJsonConstants {
protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);
@Autowired
private RepositoryService repositoryService;
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.OK)
public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap<String, String> values) {
try {
Model model = repositoryService.getModel(modelId);
ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
modelJson.put(MODEL_NAME, values.getFirst("name"));
modelJson.put(MODEL_DESCRIPTION, values.getFirst("description"));
model.setMetaInfo(modelJson.toString());
model.setName(values.getFirst("name"));
repositoryService.saveModel(model);
repositoryService.addModelEditorSource(model.getId(), values.getFirst("json_xml").getBytes("utf-8"));
InputStream svgStream = new ByteArrayInputStream(values.getFirst("svg_xml").getBytes("utf-8"));
TranscoderInput input = new TranscoderInput(svgStream);
PNGTranscoder transcoder = new PNGTranscoder();
// Setup output
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
// Do the transformation
transcoder.transcode(input, output);
final byte[] result = outStream.toByteArray();
repositoryService.addModelEditorSourceExtra(model.getId(), result);
outStream.close();
} catch (Exception e) {
LOGGER.error("Error saving model", e);
throw new ActivitiException("Error saving model", e);
}
}
}
import java.io.ByteArrayInputStream;
导入java.io.ByteArrayOutputStream;
导入java.io.InputStream;
导入org.activiti.editor.constants.ModelDataJsonConstants;
导入org.activiti.engine.activitieException;
导入org.activiti.engine.RepositoryService;
导入org.activiti.engine.repository.Model;
导入org.apache.batik.transcoder.TranscoderInput;
导入org.apache.batik.transcoder.TranscoderOutput;
导入org.apache.batik.transcoder.image.PNGTranscoder;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.HttpStatus;
导入org.springframework.util.MultiValueMap;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseStatus;
导入org.springframework.web.bind.annotation.RestController;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.databind.node.ObjectNode;
/**
*@author Tijs Rademakers
*/
@RestController
公共类ModelSaveRestResource实现ModelDataJsonConstants{
受保护的静态最终记录器Logger=LoggerFactory.getLogger(ModelSaveRestResource.class);
@自动连线
私人寄存服务寄存服务;
@自动连线
私有对象映射器对象映射器;
@RequestMapping(value=“/model/{modelId}/save”,method=RequestMethod.PUT)
@ResponseStatus(值=HttpStatus.OK)
public void saveModel(@PathVariable String modelId,@RequestBody多值映射值){
试一试{
Model Model=repositoryService.getModel(modelId);
ObjectNode modelJson=(ObjectNode)objectMapper.readTree(model.getMetaInfo());
modelJson.put(MODEL_NAME,values.getFirst(“NAME”);
modelJson.put(MODEL_DESCRIPTION,values.getFirst(“DESCRIPTION”);
setMetaInfo(modelJson.toString());
model.setName(values.getFirst(“name”));
repositoryService.saveModel(model);
repositoryService.addModelEditorSource(model.getId(),values.getFirst(“json_xml”).getBytes(“utf-8”);
InputStream svgStream=newbytearrayinputstream(values.getFirst(“svg_xml”).getBytes(“utf-8”);
TranscodeInput输入=新的TranscodeInput(svgStream);
PNGTranscoder transcoder=新PNGTranscoder();
//设置输出
ByteArrayOutputStream outStream=新建ByteArrayOutputStream();
转码输出=新转码输出(超出流);
//进行转换
转码器。转码(输入、输出);
最终字节[]结果=outStream.toByteArray();
repositoryService.addModelEditorSourceExtra(model.getId(),result);
exptream.close();
}捕获(例外e){
记录器错误(“错误保存模型”,e);
抛出新的ActivityException(“错误保存模型”,e);
}
}
}
上面的代码在Spring上运行得很好。但是在SpringBoot上运行得很好。所以我很困惑 我不认为您的数据对象被转换为字符串作为有效负载。 尝试:
data:JSON.stringify(jdata)
此外,内容类型应为:
contentType:“application/json;charset=utf-8”
我认为您的数据对象没有转换为字符串作为有效负载。
尝试:
data:JSON.stringify(jdata)
此外,内容类型应为:
contentType:“application/json;charset=utf-8”
更改ajax中的数据类型参数。您的代码指定的数据类型为JSON,但控制器的获取方式为application/x-www-form-urlencoded
您可以将其设置为数据类型:“文本”
或者,如果对请求使用JSON,则更改以下内容
consumes=MediaType.APPLICATION\u JSON\u VALUE
contentType中:“application/json”
在ajax中更改数据类型参数。您的代码指定的数据类型为JSON,但控制器的获取方式为
application/x-www-form-urlencoded
您可以将其设置为数据类型:“文本”
或者,如果对请求使用JSON,则更改以下内容
consumes=MediaType.APPLICATION\u JSON\u VALUE
contentType中:“application/json”
在这个测试示例中,我只给出了一个简单的数据。实际上,我想将一个更复杂的数据放到我的服务器上。例如,我还想从svg文件中放入一个json_xml和svg_xml。因此,我使用contextType应用程序/x-www-form-urlencoded。正如我提到的,“test1”可以得到正确的结果。问题是当我使用PUT和application/x-www-form-urlencoded时,我得到了错误。在这个测试示例中,我只给出了一个简单的数据。实际上,我想将一个更复杂的数据放入我的服务器。例如,我还想从svg文件中放入json_xml和svg_xml。因此我使用contextType应用程序/x-www-form-urlencoded。正如我所提到的,“test1”可以得到正确的结果。问题是当我使用put和application时
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* @author Tijs Rademakers
*/
@RestController
public class ModelSaveRestResource implements ModelDataJsonConstants {
protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);
@Autowired
private RepositoryService repositoryService;
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.OK)
public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap<String, String> values) {
try {
Model model = repositoryService.getModel(modelId);
ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
modelJson.put(MODEL_NAME, values.getFirst("name"));
modelJson.put(MODEL_DESCRIPTION, values.getFirst("description"));
model.setMetaInfo(modelJson.toString());
model.setName(values.getFirst("name"));
repositoryService.saveModel(model);
repositoryService.addModelEditorSource(model.getId(), values.getFirst("json_xml").getBytes("utf-8"));
InputStream svgStream = new ByteArrayInputStream(values.getFirst("svg_xml").getBytes("utf-8"));
TranscoderInput input = new TranscoderInput(svgStream);
PNGTranscoder transcoder = new PNGTranscoder();
// Setup output
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
// Do the transformation
transcoder.transcode(input, output);
final byte[] result = outStream.toByteArray();
repositoryService.addModelEditorSourceExtra(model.getId(), result);
outStream.close();
} catch (Exception e) {
LOGGER.error("Error saving model", e);
throw new ActivitiException("Error saving model", e);
}
}
}