Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带restful服务的Spring boot_Java_Spring_Rest_Maven_Spring Mvc - Fatal编程技术网

Java 带restful服务的Spring boot

Java 带restful服务的Spring boot,java,spring,rest,maven,spring-mvc,Java,Spring,Rest,Maven,Spring Mvc,我是新来的Spring boot 我正在尝试编写一个使用post请求作为输入的代码 当我试图通过post rest客户端chrome插件发送post请求时,我遇到了这个错误 我正在使用Maven构建工具 { "timestamp": 1431079188726, "status": 415, "error": "Unsupported Media Type", "exception": "org.springframework.web.HttpMediaTypeN

我是新来的
Spring boot

我正在尝试编写一个使用post请求作为输入的代码

当我试图通过post rest客户端chrome插件发送post请求时,我遇到了这个错误

我正在使用Maven构建工具

{
    "timestamp": 1431079188726,
    "status": 415,
    "error": "Unsupported Media Type",
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
    "message": "Content type 'text/plain;charset=UTF-8' not supported",
    "path": "/api/greetings" }
这些是课程

Application.java

@SpringBootApplication
@ComponentScan(basePackages="webapi")
public class Application {


    public static void main(String[] args) throws Exception{
        SpringApplication.run(Application.class, args);

    }

}
public class Greeting {

    private BigInteger id;
    private String text;

    public Greeting() {

    }

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}
@RestController

public class GreetingController {



    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;


    private static Greeting save(Greeting greeting) {
        if (greetingMap == null) {
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;

        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World");
        save(g1);

        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo!");
        save(g2);

    }



    @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Collection<Greeting>> getGreetings() {
        Collection<Greeting> greetings = greetingMap.values();

        return new ResponseEntity<Collection<Greeting>>(greetings,
                HttpStatus.OK);
    }


    @RequestMapping(value = "/api/greetings/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Greeting> getGreeting(
            @PathVariable("id") BigInteger id)
    {
        Greeting greeting = greetingMap.get(id);
        if (greeting == null) {
            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);

    }


    @RequestMapping(value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<Greeting> createGreeting(
            @RequestBody Greeting greeting) {
        Greeting savedGreeting = save(greeting);
        return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);
    }

}
Greeting.java

@SpringBootApplication
@ComponentScan(basePackages="webapi")
public class Application {


    public static void main(String[] args) throws Exception{
        SpringApplication.run(Application.class, args);

    }

}
public class Greeting {

    private BigInteger id;
    private String text;

    public Greeting() {

    }

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}
@RestController

public class GreetingController {



    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;


    private static Greeting save(Greeting greeting) {
        if (greetingMap == null) {
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;

        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World");
        save(g1);

        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo!");
        save(g2);

    }



    @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Collection<Greeting>> getGreetings() {
        Collection<Greeting> greetings = greetingMap.values();

        return new ResponseEntity<Collection<Greeting>>(greetings,
                HttpStatus.OK);
    }


    @RequestMapping(value = "/api/greetings/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Greeting> getGreeting(
            @PathVariable("id") BigInteger id)
    {
        Greeting greeting = greetingMap.get(id);
        if (greeting == null) {
            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);

    }


    @RequestMapping(value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<Greeting> createGreeting(
            @RequestBody Greeting greeting) {
        Greeting savedGreeting = save(greeting);
        return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);
    }

}
GreetingController.java

@SpringBootApplication
@ComponentScan(basePackages="webapi")
public class Application {


    public static void main(String[] args) throws Exception{
        SpringApplication.run(Application.class, args);

    }

}
public class Greeting {

    private BigInteger id;
    private String text;

    public Greeting() {

    }

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}
@RestController

public class GreetingController {



    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;


    private static Greeting save(Greeting greeting) {
        if (greetingMap == null) {
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;

        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World");
        save(g1);

        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo!");
        save(g2);

    }



    @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Collection<Greeting>> getGreetings() {
        Collection<Greeting> greetings = greetingMap.values();

        return new ResponseEntity<Collection<Greeting>>(greetings,
                HttpStatus.OK);
    }


    @RequestMapping(value = "/api/greetings/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Greeting> getGreeting(
            @PathVariable("id") BigInteger id)
    {
        Greeting greeting = greetingMap.get(id);
        if (greeting == null) {
            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);

    }


    @RequestMapping(value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<Greeting> createGreeting(
            @RequestBody Greeting greeting) {
        Greeting savedGreeting = save(greeting);
        return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);
    }

}
@RestController
公共类迎宾控制器{
私有静态大整数nextId;
私有静态映射迎宾映射;
专用静态问候语保存(问候语){
if(greetingMap==null){
greetingMap=新HashMap();
nextId=biginger.ONE;
}
问候语.setId(nextId);
nextId=nextId.add(biginger.ONE);
greetingMap.put(greeting.getId(),greeting);
回敬问候;
}
静止的{
问候语g1=新问候语();
g1.setText(“你好世界”);
保存(g1);
问候语g2=新问候语();
g2.setText(“Hola Mundo!”);
保存(g2);
}
@RequestMapping(value=“/api/greetings”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
公众反应{
集合问候语=greetingMap.values();
返回新的响应(问候,
HttpStatus.OK);
}
@RequestMapping(value=“/api/greats/{id}”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
公众反应(
@PathVariable(“id”)BigInteger(整数id)
{
Greeting Greeting=greetingMap.get(id);
if(问候语==null){
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
返回新的响应(问候语,HttpStatus.OK);
}
@RequestMapping(value=“/api/greetings”,method=RequestMethod.POST,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
@应答器
公众反应(
@请求者(身体问候语){
问候语savedGreeting=保存(问候语);
返回新的ResponseEntity(savedGreeting,HttpStatus.CREATED);
}
}

您已经将路径的映射定义为
/api/greetings
(带有参数)consumes=MediaType。应用程序\u JSON\u VALUE因此编译器正在等待JSON输入,但它正在获取类型为
text/plain
(它在堆栈跟踪中提到)的请求,所以无论从何处传递请求,都要确保内容类型为“application/json”

由于类型
text/plain
/api/greetings
没有映射,因此引发此异常

希望有帮助


祝你好运!

该服务需要一个
应用程序/json
内容类型
,但它接收到一个
文本/普通
输入


你用什么来制作帖子?如果你在用Postman进行测试,请确保在标题部分添加
内容类型
应用程序/json
值。

@KunalMalhotra有帮助吗?