簡體   English   中英

春假休息服務

[英]Spring Rest Service

我正在使用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

我的控制器:

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服務之后,我添加了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);
    }
}

您可能有JsonMappingException。

我認為你需要添加getter / setter或公開一些屬性,因為在序列化你的Customer類時Jackson可能會感到困惑。

嘗試定義方法和響應類型。

@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方法使用@ResponseBody注釋,如下所示

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

它用於對象序列化。 它將嘗試轉換返回值(客戶對象)並自動將其寫入http響應。

我的印象是,客戶的私人領域缺乏吸氣劑可能會造成麻煩

此外,您是否在pom.xml中添加了JSON路徑依賴項?

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

你在使用Spring Boot嗎? 如果是,則應使用@SpringBootApplication注釋Application類。 這捆綁了@EnableWebMvc@Configuration @EnableWebMvc@ComponentScan @EnableAutoConfiguration@EnableAutoConfiguration注釋。 堆棧跟蹤有助於確定問題究竟是什么。

改變你的

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

  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