2016-08-12 31 views
2

使用Spring Boot,我想构建一个Rest控制器。但是Spring未能自动将Json转换为Java对象。是的:Spring Boot REST控制器没有将给定的Json参数解析为Java对象

  • 我有空的构造
  • 我试过@RequestParam,@RequestBody,@RequestPart
  • 我的JSON参数是有效的(我试着用GSON解析,和它的工作)

这里是我的休息控制器:

@RestController 
public class HelloController { 

    @RequestMapping(value= "/abc", method=RequestMethod.POST) 
    @ResponseStatus(HttpStatus.OK) 
    public @ResponseBody MyResponse handlePurchase(@RequestParam A request){ 
     return new MyResponse("Simply the best"); 
    } 

} 

这里是A.java:

public class A implements Serializable { 

    private B something; 

    public B getSomething() { 
     return Something; 
    } 

    public void setSomething(B something) { 
     this.Something = something; 
    } 

    @Override 
    public String toString() { 
     return "A [Something=" + something + "]"; 
    } 

    public A(B something) { 
     super(); 
     this.Something = something; 
    } 

    public A() { 
     super(); 
    } 


} 

下面是B.java它使用A.java:

public class B implements Serializable { 
    private String something; 
    private int catsNumber; 
    private int dogsNumber; 

    public String getSomething() { 
     return something; 
    } 

    public void setSomething(String something) { 
     this.something = something; 
    } 

    public int getCatsNumber() { 
     return catsNumber; 
    } 

    public void setCatsNumber(int catsNumber) { 
     this.catsNumber = catsNumber; 
    } 

    public int getDogsNumber() { 
     return dogsNumber; 
    } 

    public void setDogsNumber(int dogsNumber) { 
     this.dogsNumber = dogsNumber; 
    } 

    @Override 
    public String toString() { 
     return "B [something=" + something + ", catsNumber=" + catsNumber + ", dogsNumber=" + dogsNumber + "]"; 
    } 

    public B() { 
     super(); 
    } 


} 

希望有人能帮助我在这,但都不曾:Spring.io

    • 其他Stackoverflow Q &由于
  • +0

    您是否有任何异常? –

    +0

    你能分享你的消息正文json吗? – Rohit

    +0

    您可以发布您的pom.xml – kuhajeyan

    回答

    2

    @RequestParam表示url参数http://foo.com?parameter=x。在使用后,您需要定义@RequestBody

    @RequestMapping(value= "/abc", method=RequestMethod.POST) 
    @ResponseStatus(HttpStatus.OK) 
    public @ResponseBody MyResponse handlePurchase(@RequestBody A a){ 
        return new MyResponse("Simply the best"); 
    } 
    
    +0

    谢谢,但仍然没有工作(在更改“@RequestParam”为“@RequestBody”,发布对我来说很好): 这里是(www-form-urlencoded) { “timestamp”:1470992850627, “status”:415, “error”:“Unsupported Media T ype“, ”exception“:”org.springframework.web.HttpMediaTypeNotSupportedException“, ”message“:”Content type'application/x-www-form-urlencoded; charset = UTF-8'not supported“, ”path “:”/ purchase“ } – Janekx

    +0

    而简单的form-data:”org.springframework.http.converter.HttpMessageNotReadableException“, ”message“:”无法读取文档:意外字符(' - '(code 45))在数值中:期望的数字(0-9)跟随负号,对于有效的数字值\ n在[Source:[email protected];行:1,列:3];嵌套的异常是com.fasterxml.jackson.core.JsonParseException:数字值中的意外字符(' - '(代码45)):预期数字(0-9)跟随减号,对于有效数值\ n [Source: [email protected]; line:1,column:3] ...} – Janekx

    +0

    你用什么工具生成你的帖子?你可以分享你的应用程序(github/bitbucket/...) – JohanB