简体   繁体   English

春假休息服务

[英]Spring Rest Service

I'm working with example from https://spring.io/guides/gs/rest-service/ and I changed default class Greeting to my own class and, when I'm calling this in web browser http://localhost:8080/greeting I get answer: There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation 我正在使用https://spring.io/guides/gs/rest-service/中的示例,我将默认类Greeting更改为我自己的类,当我在Web浏览器中调用它时http:// localhost: 8080 /问候我得到答案: There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation

My controller: 我的控制器:

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

And customer class: 和客户类:

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

following Spring MVC Rest / JSON service I added jackson mapper dependency but it doesn't works... Spring MVC Rest / JSON服务之后,我添加了jackson mapper依赖,但它不起作用......

Please help me 请帮我

According to nebula comment. 根据星云的评论。 I haven't web.xml, I'm using example from https://spring.io/guides/gs/rest-service/ and there isn't any. 我没有web.xml,我使用的是https://spring.io/guides/gs/rest-service/中的示例,但没有。

Here is my Application.class 这是我的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);
    }
}

You probably have a JsonMappingException. 您可能有JsonMappingException。

I think you need to add getters/setters or make some attributes public, because Jackson is probably confused when serializing your Customer class. 我认为你需要添加getter / setter或公开一些属性,因为在序列化你的Customer类时Jackson可能会感到困惑。

Try to define method and response type. 尝试定义方法和响应类型。

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

Also make sure it is correctly mapped as http://localhost:8080/greeting 还要确保将其正确映射为http://localhost:8080/greeting

You have to use @ResponseBody annotation for your greeting method as following 您必须为您的greeting方法使用@ResponseBody注释,如下所示

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

It is used for object serialization. 它用于对象序列化。 It will try to convert return value (customer object) and write it to the http response automatically. 它将尝试转换返回值(客户对象)并自动将其写入http响应。

I am under the impression the lack of getters for your private fields in your Customer might cause the trouble 我的印象是,客户的私人领域缺乏吸气剂可能会造成麻烦

Besides, have you added the JSON path dependency in your pom.xml? 此外,您是否在pom.xml中添加了JSON路径依赖项?

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

Are you using Spring Boot? 你在使用Spring Boot吗? If you are, your Application class should be annotated with @SpringBootApplication . 如果是,则应使用@SpringBootApplication注释Application类。 This bundles the @EnableWebMvc , @Configuration , @ComponentScan and @EnableAutoConfiguration annotations. 这捆绑了@EnableWebMvc@Configuration @EnableWebMvc@ComponentScan @EnableAutoConfiguration@EnableAutoConfiguration注释。 A stacktrace would be helpful to try figure out exactly what the issue is. 堆栈跟踪有助于确定问题究竟是什么。

change your 改变你的

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

To

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM