【问题标题】:Spring Rest Service春季休息服务
【发布时间】:2015-03-15 20:51:28
【问题描述】:

我正在使用来自 https://spring.io/guides/gs/rest-service/ 的示例,并将默认类 Greeting 更改为我自己的类,当我在网络浏览器 http://localhost:8080/greeting 中调用它时,我得到了答案: There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation

我的控制器:

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);
    }
}

Spring MVC Rest / JSON service 之后,我添加了jackson mapper 依赖项,但它不起作用...

请帮帮我

根据星云评论。 我没有 web.xml,我使用的是来自 https://spring.io/guides/gs/rest-service/ 的示例,但没有。

这是我的 Application.class

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);
    }
}

【问题讨论】:

  • 你有web.xml吗?也没有为组件扫描提供包。如果 rest 是您的基本包,则应该类似于 @ComponentScan("rest")
  • 不,我没有 web.xml。更改为 @ComponentScan("rest") 后,它仍然给出相同的错误... :(
  • 如果你没有 web.xml 那么应该有一个实现WebApplicationInitializer的类。你有这样的课吗?
  • 等一下。您是否按照该教程尝试使用 Spring Boot?
  • 我正在关注本教程,并且在 pom.xml 中我有 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> 所以我猜是的,我正在使用 Spring boot

标签: java json spring rest spring-mvc


【解决方案1】:

您可能遇到了 JsonMappingException。

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

【讨论】:

  • 试试这个,添加公共吸气剂。我觉得布赖恩是对的。
【解决方案2】:

尝试定义方法和响应类型。

@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

【讨论】:

  • 还是同样的错误。当我使用 Greeting 类(默认)时,它可以正常工作,所以我认为它映射正确
  • 您能否使用web.xml 或适用于您的案例的应用程序配置类更新您的问题?
【解决方案3】:

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

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

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

【讨论】:

  • 已经是@RestController了,不用加@ResponseBody
【解决方案4】:

我的印象是您的客户中的私有字段缺少吸气剂可能会导致麻烦

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

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

【讨论】:

    【解决方案5】:

    您使用的是 Spring Boot 吗?如果你是,你的应用程序类应该用@SpringBootApplication 注释。这捆绑了@EnableWebMvc@Configuration@ComponentScan@EnableAutoConfiguration 注释。堆栈跟踪有助于尝试找出问题所在。

    【讨论】:

      【解决方案6】:

      改变你的

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

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

      【讨论】:

        猜你喜欢
        • 2016-11-13
        • 2018-06-19
        • 1970-01-01
        • 2018-03-16
        • 2013-03-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-25
        相关资源
        最近更新 更多