Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.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 春假服务_Java_Json_Spring_Rest_Spring Mvc - Fatal编程技术网

Java 春假服务

Java 春假服务,java,json,spring,rest,spring-mvc,Java,Json,Spring,Rest,Spring Mvc,我正在使用来自的示例,我将默认的类问候语更改为我自己的类,当我在web浏览器中调用此选项时,我得到的答案是: 出现意外错误(类型=不可接受,状态=406)。 找不到可接受的表示形式 我的控制器: package rest; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework

我正在使用来自的示例,我将默认的类问候语更改为我自己的类,当我在web浏览器中调用此选项时,我得到的答案是:
出现意外错误(类型=不可接受,状态=406)。
找不到可接受的表示形式

我的控制器:

package rest;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import database.*;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();
    private DBAccess dbaccess= new DBAccess();

    @RequestMapping("/greeting")
    public Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new  Customer(1,"a","b");
    }
}
客户类别:

package database;

public class Customer {
    private long id;
    private  String firstName, lastName;

    public Customer(){};
    public Customer(long id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }


    void setFirstName(String firstName){
        this.firstName=firstName;
    }

    void setLastName(String lastName){
        this.lastName=lastName;
    }


    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}
下面我添加了jackson mapper依赖项,但它不起作用

请帮帮我

根据星云评论。 我没有web.xml,我使用的是来自的示例,没有

这是我的申请表

package rest;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

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

尝试定义方法和响应类型

@RequestMapping(value = "/greeting", method = RequestMethod.GET, 
produces = "application/json; charset=utf-8")
@ResponseBody
public Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {

还要确保它正确映射为
http://localhost:8080/greeting

您必须为您的
问候语
方法使用
@ResponseBody
注释,如下所示

 @RequestMapping("/greeting") 
 public @ResponseBody Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {
     return new  Customer(1,"a","b"); 
 } 

它用于对象序列化。它将尝试转换返回值(客户对象)并将其自动写入http响应

您可能有一个JsonMappingException

我认为您需要添加getter/setter或公开某些属性,因为Jackson在序列化客户类时可能会感到困惑。

更改您的

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


我的印象是,在你的客户中,你的私人领域缺少getter可能会造成麻烦

此外,您是否在pom.xml中添加了JSON路径依赖项

 <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <scope>test</scope>
 </dependency>

com.jayway.jsonpath
json路径
测试

您正在使用Spring Boot吗?如果是,应用程序类应该用
@springbootplication
注释。这捆绑了
@EnableWebMvc
@Configuration
@ComponentScan
@EnableAutoConfiguration
注释。stacktrace将有助于准确地找出问题所在。

仍然是相同的错误。当我使用问候语类(默认)时,它工作正常,因此我认为它映射正确。您可以使用
web.xml
或适用于您案例的应用程序配置类更新您的问题吗?您是否有
web.xml
?此外,没有为组件扫描提供软件包。如果rest是您的基本包,则应该类似于
@ComponentScan(“rest”)
。不,我没有web.xml。更改为@ComponentScan(“rest”)后,仍然会出现相同的错误…:(如果你没有web.xml,那么应该有一个实现
WebApplicationInitializer
的类。你有这样的类吗?等等。你是在那个教程之后尝试Spring boot的吗?我正在学习这个教程,在pom.xml中我有
org.springframework.boot-Spring boot-maven插件
,所以我想是的,我正在使用Spring bootIt已经是一个
@RestController
,不需要添加
@ResponseBody
试试看,添加公共getter。我认为Brian是对的。
 <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <scope>test</scope>
 </dependency>